Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在global.asax中访问过的POST请求在CustomAuthorization类-WebAPI中无法访问_C#_Asp.net Web Api2 - Fatal编程技术网

C# 在global.asax中访问过的POST请求在CustomAuthorization类-WebAPI中无法访问

C# 在global.asax中访问过的POST请求在CustomAuthorization类-WebAPI中无法访问,c#,asp.net-web-api2,C#,Asp.net Web Api2,我正在访问WebAPI中global.asax文件的应用程序_beginrequest方法中的POST请求。一旦我从请求中获取了UserID,我就尝试再次从HttpActionContext方法获取相同的请求,并尝试对用户进行自定义授权,但是一旦我在global.asax文件中获取了数据,CustomAuthorization类中就不再有参数可用了。不确定这是为什么发生的,以及这是否是有意的行为。有人能解释一下发生了什么事吗。请在下面找到我的代码 Global.asax protecte

我正在访问WebAPI中global.asax文件的应用程序_beginrequest方法中的POST请求。一旦我从请求中获取了UserID,我就尝试再次从HttpActionContext方法获取相同的请求,并尝试对用户进行自定义授权,但是一旦我在global.asax文件中获取了数据,CustomAuthorization类中就不再有参数可用了。不确定这是为什么发生的,以及这是否是有意的行为。有人能解释一下发生了什么事吗。请在下面找到我的代码

Global.asax

    protected void Application_BeginRequest(Object source, EventArgs e)
    {
        var userID = GetUserID(HttpContext.Current);
        try
        {
         CustomAuthorizeAttribute customObj= new CustomAuthorizeAttribute();

            if (!customObj.AuthorizeRequest(userID))
            {
                throw new Exception();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

 private string GetUserID(HttpContext context)
    {
        string UserID = string.Empty;

        using (var stream= new StreamReader(context.Request.InputStream))
        {
            string inputData = stream.ReadToEnd();
           //code to parse the data and get the userID value.                         
        }
        return userID;
    }
自定义属性.cs

 public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
       userID = GetUser(actionContext);

            if (CustomAuthorize(userID))
            {
                return;
            }                         
    }

 private string GetUser(System.Web.Http.Controllers.HttpActionContext actionContext)
    {            
       var username = string.Empty;            
        var request = actionContext.Request.Content.ReadAsStringAsync().Result;
        if ((request != null) && (request != string.Empty))
        {
            JObject Obj = JObject.Parse(request);
            if (Obj != null)
                username = (string)Obj ["userID"];                
        }
        return username;
    }

我在一篇帖子中找到了答案。流本身只能读取一次,因此,我将其复制到内存流中

 using (var stream = new MemoryStream())
        {
            context.Request.InputStream.Seek(0, SeekOrigin.Begin);
            context.Request.InputStream.CopyTo(stream);
            string requestBody = Encoding.UTF8.GetString(stream.ToArray());
            if(requestBody!=string.Empty)
            {
                JObject inputReqObj = JObject.Parse(requestBody);
                UserID = (string)inputReqObj["eUserID"];
            }
        }         
现在,我仍然可以阅读我的CustomAuthorization类中的问题。我唯一需要做的更改是在global.asax文件中。在下面的帖子中找到了这个答案,对它进行了一些调整以满足我的要求。

老兄,我已经连续24小时把头撞在墙上,几乎放弃了希望。在这里,您得到了完全正确的答案,只使用了四行代码(不包括我不需要的“if”语句)。没有一个投票,没有一个评论,没有一个不是你自己的答案。至少直到现在。我觉得我刚刚发现了埋藏在海底的无价之宝非常感谢你!我不敢相信这件事没有得到更多的关注。我会试着把这篇文章传播给其他烦恼的人。:)