ASP.NET Core6 Filter中获取Body中的参数

   [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public class MyFilterAttribute : Attribute, IAsyncAuthorizationFilter
    {
        private readonly IConfiguration configuration;

        public MyFilterAttribute(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            var request = context.HttpContext.Request;
          
       //开启多次读取body流 request.EnableBuffering();
      //leaveOpen:true标识StreamReader释放时不会自动关闭流   
using var sr = new StreamReader(request.Body, leaveOpen: true, encoding: Encoding.UTF8);
var paramJson = await sr.ReadToEndAsync();
       //Action中可再次读取流 request.Body.Seek(
0, SeekOrigin.Begin); } }

 

posted @ 2021-12-11 10:42  b̶i̶n̶g̶.̶  阅读(50)  评论(0编辑  收藏  举报