Read request body twice

后端 未结 3 1746
傲寒
傲寒 2020-12-03 13:30

I am trying to read the body in a middleware for authentication purposes, but when the request gets to the api controller the object is empty as the body has already been re

3条回答
  •  鱼传尺愫
    2020-12-03 14:08

    This works with .Net Core 2.1 and higher.

    Today I run in a similar issue. Long story short, what used to work with

    Body.Seek(0, SeekOrigin.Begin);
    

    resulted in today in exception, at least in my case. This happened after the code was migrated to the latest version of .NET Core.

    The workaround for me was to add this:

    app.Use(next => context => { context.Request.EnableBuffering(); return next(context);
    

    Add this before setting up controllers or MVC. This seems to be added as part of the .NET Core 2.1 version.

    Hope this helps someone!

    Cheers and happy coding.

提交回复
热议问题