【发布时间】:2014-01-29 20:09:37
【问题描述】:
我正在使用this 从输入流中获取请求参数。 POST 在请求正文中使用 JSON。在我的onAuthorize 函数中,它覆盖了AuthorizeAttribute。它确实给了我请求主体参数,但它清空了流,因此控制器没有收到任何请求参数:
public override void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.HttpContext.Request.InputStream.Length() //17 here
string jsonPostData;
using (var stream = filterContext.HttpContext.Request.InputStream)
{
stream.Position = 0;
using (var reader = new System.IO.StreamReader(stream))
{
jsonPostData = reader.ReadToEnd();
}
}
filterContext.HttpContext.InputStream.Length() //0 here
filterContext.HttpContext.Request.InputStream.Position = 0; // still 0
base.OnAuthorization(filterContext); //so when the request reaches controller its empty
}
我想我本质上要问的是如何在阅读后重置输入流
【问题讨论】:
标签: c# asp.net-mvc json authentication