获取定制AuthorizeAttribute后请求参数 [英] Get Post request parameters in custom AuthorizeAttribute

查看:2347
本文介绍了获取定制AuthorizeAttribute后请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从输入流中获取请求参数。 POST在请求体使用JSON。在我的 onAuthorize 函数,它是为overrride AuthorizeAttribute 。它确实给我请求体的参数,但它清空出流,以控制器没有收到任何请求参数:

I am using this to get request parameters from input stream. POST is using JSON in the request body. In my onAuthorize function which is overrride for AuthorizeAttribute. It does give me the request body parameters, but it empties out stream so controller does not receive any request parameters:

 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
}

我猜我本质要求是如何读取输入流后重置它

I guess what i am essentially asking is how to reset input stream after reading it

推荐答案

改code到这一点,它开始工作。

Changed code to this and it started working

public override void OnAuthorization(AuthorizationContext filterContext)
    {
        filterContext.HttpContext.Request.InputStream.Length() //17 here
        string jsonPostData;
        var stream = request.InputStream;
        var reader = new System.IO.StreamReader(stream);
        jsonPostData = reader.ReadToEnd();
        filterContext.HttpContext.InputStream.Length() //17 here
        filterContext.HttpContext.Request.InputStream.Position = 0; //17 here

  base.OnAuthorization(filterContext); //so when the request reaches controller its empty
}

这篇关于获取定制AuthorizeAttribute后请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆