I am trying to capture the body of a request in an authorization class (AuthorizationHandler), however because this body is a Stream, after reading its contents the next post request can not be executed because the stream has already been read and its contents no longer exist.
I'm using the following code:
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, Autorizacao requirement)
{
var routeValues = context.Resource as AuthorizationFilterContext;
if (routeValues != null)
{
var obj = StreamToObject(routeValues.HttpContext.Request.Body);
context.Succeed(requirement);
}
return Task.FromResult(0);
}
private Object StreamToObject(Stream stream)
{
try
{
string content;
using (var reader = new StreamReader(stream))
content = reader.ReadToEnd();
return Newtonsoft.Json.JsonConvert.DeserializeObject(content);
}
catch (Exception e)
{
throw e;
}
}
I know this is happening because after reading the stream your content is being discarded, but I would like to know what I can do to get around this problem?