Read JSON post data in ASP.Net Core MVC

前端 未结 1 1933
有刺的猬
有刺的猬 2021-01-04 11:17

I\'ve tried to find a solution for this, but all the ones coming up are for previous versions of ASP.Net.

I\'m working with the JWT authentication middleware and hav

相关标签:
1条回答
  • 2021-01-04 11:35

    By the time it gets to your middleware the request stream has already been read, so what you can do here is Microsoft.AspNetCore.Http.Internal.EnableRewind on the Request and read it yourself

    Site wide :

    Startup.cs
    using Microsoft.AspNetCore.Http.Internal;
    
    Startup.Configure(...){
    ...
    //Its important the rewind us added before UseMvc
    app.Use(next => context => { context.Request.EnableRewind(); return next(context); });
    app.UseMvc()
    ...
    }
    

    OR selective :

    private async Task GenerateToken(HttpContext context)
        {
         context.Request.EnableRewind();
         string jsonData = new StreamReader(context.Request.Body).ReadToEnd();
        ...
        }
    
    0 讨论(0)
提交回复
热议问题