I have some ASP.NET Core middleware (an AuthorizationHandler
) that needs to access the body of the request to perform an authorization check. I use the new pipelines APIs to access the request body. The code looks like this:
PipeReader bodyReader = httpContext.Request.BodyReader;
ReadResult readResult = await bodyReader.ReadAsync();
ReadOnlySequence<byte> readResultBuffer = readResult.Buffer;
var utf8JsonReader = new Utf8JsonReader(bytes);
while (utf8JsonReader.Read()) { ... }
When this code has run, the body stream is read. The controller that I want to call throws a validation error because it doesn't see a request body because it was already read.
So how do I reset the PipeReader
so that the request body can be re-read?
I know that when you do not use BodyReader
but Body
, you can use EnableBuffering
to enable request re-reads. However, when using pipelines, this no longer works (or I'm doing something else wrong).