我试图读取中间件中的主体以进行身份验证,但是当请求到达 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 );
如果您正在使用application/x-www-form-urlencoded
or multipart/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;
}
});
您还可以使用BufferingHelper.EnableRewind()
扩展,它是Microsoft.AspNet.Http
包:它基于类似的方法,但依赖于一个特殊的流,该流开始在内存中缓冲数据,并在达到阈值时将所有内容假脱机到磁盘上的临时文件:
app.Use(next => context =>
{
context.Request.EnableRewind();
return next(context);
});
仅供参考:将来可能会在 vNext 中添加缓冲中间件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)