读取请求正文两次

Jak*_*ote 19 asp.net-core-mvc asp.net-core asp.net-core-webapi

我试图在中间件中读取正文以进行身份​​验证,但是当请求到达api控制器时,对象是空的,因为正在读取正文.有没有办法解决.我正在中间件中读这样的身体.

var buffer = new byte[ Convert.ToInt32( context.Request.ContentLength ) ];
await context.Request.Body.ReadAsync( buffer, 0, buffer.Length );
var body = Encoding.UTF8.GetString( buffer );
Run Code Online (Sandbox Code Playgroud)

Kév*_*let 35

如果您正在使用application/x-www-form-urlencodedmultipart/form-data,则可以安全地context.Request.ReadFormAsync()多次调用,因为它会在后续调用中返回缓存实例.

如果您使用的是其他内容类型,则必须手动缓冲请求并使用可重绕的流替换请求正文MemoryStream.以下是使用内联中间件的方法(您需要在管道中尽快注册):

app.Use(next => async context =>
{
    // Keep the original stream in a separate
    // variable to restore it later if necessary.
    var stream = context.Request.Body;

    // Optimization: don't buffer the request if
    // there was no stream or if it is rewindable.
    if (stream == Stream.Null || stream.CanSeek)
    {
        await next(context);

        return;
    }

    try
    {
        using (var buffer = new MemoryStream())
        {
            // Copy the request stream to the memory stream.
            await stream.CopyToAsync(buffer);

            // Rewind the memory stream.
            buffer.Position = 0L;

            // Replace the request stream by the memory stream.
            context.Request.Body = buffer;

            // Invoke the rest of the pipeline.
            await next(context);
        }
    }

    finally
    {
        // Restore the original stream.
        context.Request.Body = stream;
    }
});
Run Code Online (Sandbox Code Playgroud)

您还可以使用BufferingHelper.EnableRewind()扩展,它是Microsoft.AspNet.Http程序包的一部分:它基于类似的方法,但依赖于一个特殊的流,它开始缓冲内存中的数据,并在达到阈值时将所有内容假脱机到磁盘上的临时文件:

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

    return next(context);
});
Run Code Online (Sandbox Code Playgroud)

仅供参考:将来可能会将缓冲中间件添加到vNext.

  • 我对缓存感到好奇.合同或实施细节的这一部分是否可能在未来发生变化? (2认同)
  • 在.Net Core 3.0中, context.Request.EnableRewind() 应更改为 context.Request.EnableBuffering() (2认同)

Nat*_*tta 8

PinPoint提到EnableRewind的用法

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()
...
}
Run Code Online (Sandbox Code Playgroud)

然后在你的中间件中你只是倒带并重读

private async Task GenerateToken(HttpContext context)
    {
     context.Request.EnableRewind();
     string jsonData = new StreamReader(context.Request.Body).ReadToEnd();
    ...
    }
Run Code Online (Sandbox Code Playgroud)

  • 同样在.Net Core 2.1中,必须在StreamReader之前添加context.Request.Body.Position = 0,否则你将获得""(空) (9认同)