asp.net-core – 访问Raw Request Body

前端之家收集整理的这篇文章主要介绍了asp.net-core – 访问Raw Request Body前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在ASP.net 5中访问请求的原始输入正文/流.过去,我能够将输入流的位置重置为0并将其读入内存流但是当我尝试这样做时从上下文输入流为null或抛出错误(System.NotSupportedException =>“不支持指定的方法.”).

在下面的第一个例子中,如果我将控制器方法的参数对象类型声明为动态,我可以在控制器中访问原始请求.由于各种原因,这不是一个解决方案,我需要在身份验证过滤器中访问原始请求正文.

此示例有效,但不是一个合理的解决方案:

  1. [HttpPost("requestme")]
  2. public string GetRequestBody([FromBody] dynamic body)
  3. {
  4. return body.ToString();
  5. }

引发错误

  1. [HttpPost("requestme")]
  2. public string GetRequestBody()
  3. {
  4. var m = new MemoryStream();
  5. Request.Body.CopyTo(m);
  6.  
  7. var contentLength = m.Length;
  8.  
  9. var b = System.Text.Encoding.UTF8.GetString(m.ToArray());
  10.  
  11. return b;
  12. }

引发错误

  1. [HttpPost("requestme")]
  2. public string GetRequestBody()
  3. {
  4. Request.Body.Position = 0;
  5. var input = new StreamReader(Request.Body).ReadToEnd();
  6.  
  7. return input;
  8. }

引发错误

  1. [HttpPost("requestme")]
  2. public string GetRequestBody()
  3. {
  4. Request.Body.Position = 0;
  5. var input = new MemoryStream();
  6. Request.Body.CopyTo(input);
  7.  
  8. var inputString = System.Text.Encoding.UTF8.GetString(input.ToArray());
  9.  
  10. return inputString;
  11. }

我需要访问我正在构建的API的每个请求的原始请求主体.

任何帮助或方向将不胜感激!

编辑:

这是我想要阅读请求正文的代码.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNet.Mvc;
  6. using Microsoft.AspNet.Http;
  7.  
  8. namespace API.Filters
  9. {
  10. public class CustomAuthorizationAttribute : Attribute,IAuthorizationFilter
  11. {
  12. public CustomAuthorizationAttribute()
  13. { }
  14.  
  15. public void OnAuthorization(AuthorizationContext context)
  16. {
  17. if (context == null)
  18. throw new ArgumentNullException("OnAuthorization AuthorizationContext context can not be null.");
  19. else
  20. {
  21. if (this.AuthorizeCore(context.HttpContext) == false)
  22. {
  23. // Do Other Stuff To Check Auth
  24. }
  25. else
  26. {
  27. context.Result = new HttpUnauthorizedResult();
  28. }
  29. }
  30. }
  31.  
  32. protected virtual bool AuthorizeCore(HttpContext httpContext)
  33. {
  34. var result = false;
  35.  
  36. using (System.IO.MemoryStream m = new System.IO.MemoryStream())
  37. {
  38. try
  39. {
  40. if (httpContext.Request.Body.CanSeek == true)
  41. httpContext.Request.Body.Position = 0;
  42.  
  43. httpContext.Request.Body.CopyTo(m);
  44.  
  45. var bodyString = System.Text.Encoding.UTF8.GetString(m.ToArray());
  46.  
  47. return CheckBody(bodyString); // Initial Auth Check returns true/false <-- Not Shown In Code Here on Stack Overflow
  48. }
  49. catch (Exception ex)
  50. {
  51. Logger.WriteLine(ex.Message);
  52. }
  53. }
  54. return false;
  55. }
  56. }
  57. }

调用标记有CustomAuthorization属性的控制器方法时,将访问此代码.

  1. [Filters.CustomAuthorizationAuthorization]
  2. [HttpPost]
  3. public ActionResult Post([FromBody]UserModel Profile)
  4. {
  5. // Process Profile
  6. }

解决方法

Request.Body的实现取决于控制器操作.

如果操作包含参数,则由Microsoft.AspNet.WebUtilities.FileBufferingReadStream实现,它支持搜索(Request.Body.CanSeek == true).此类型还支持设置Request.Body.Position.

但是,如果您的操作不包含任何参数,则由Microsoft.AspNet.Loader.IIS.FeatureModel.RequestBody实现,它不支持搜索(Request.Body.CanSeek == false).这意味着您无法调整Position属性,只需开始阅读流即可.

这种差异可能与MVC需要从请求体中提取参数值的事实有关,因此它需要读取请求.

在您的情况下,您的操作没有任何参数.因此使用Microsoft.AspNet.Loader.IIS.FeatureModel.RequestBody,如果您尝试设置Position属性,则会引发异常.

解决方案:要么不设置位置,要么检查您是否可以先设置位置:

  1. if (Request.Body.CanSeek)
  2. {
  3. // Reset the position to zero to read from the beginning.
  4. Request.Body.Position = 0;
  5. }
  6.  
  7. var input = new StreamReader(Request.Body).ReadToEnd();

猜你在找的asp.Net相关文章