An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hi @Shemesh, Tchelet ,
Looking at your setup, I would move the request body reading into the ASP.NET Core app rather than doing it from the classic IHttpModule.
The main reason is that IIS managed modules do not handle ASP.NET Core requests the same way they handle ASP.NET Framework requests. For ASP.NET Core apps, Microsoft points to ASP.NET Core middleware as the replacement approach for these kinds of scenarios: IIS modules with ASP.NET Core.
If you can update AzureRedirectApp, you can add middleware early in the pipeline and enable buffering before reading the body:
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
string requestBody;
using (var reader = new StreamReader(
context.Request.Body,
Encoding.UTF8,
detectEncodingFromByteOrderMarks: false,
leaveOpen: true))
{
requestBody = await reader.ReadToEndAsync();
}
context.Request.Body.Position = 0;
// Store or forward requestBody as needed.
await next();
});
The important parts are EnableBuffering() and resetting Request.Body.Position back to 0. EnableBuffering() allows the body to be read more than once, and resetting the position lets the rest of the application read the body normally afterward: EnableBuffering method.
For a setup where AzureRedirectApp cannot be modified, I do not see a Microsoft-documented approach that allows a classic managed IHttpModule to read the request body reliably without affecting the ASP.NET Core request pipeline.
GetBufferedInputStream() is documented for preserving the body for downstream ASP.NET Framework processing, but I do not see documentation showing that it is supported for an ASP.NET Core app hosted through AspNetCoreModuleV2.
One thing I noticed in the attached module is that the current code still sets requestBody to an empty string, so the body-reading code that caused the downstream issue does not appear to be included in the attachment.
One other part worth reviewing is PreSendRequestHeaders. Microsoft advises against using this event in a managed IHttpModule because it can cause issues with asynchronous requests: PreSendRequestHeaders event.
If you found my response helpful, I would greatly appreciate it if you could follow this guidance. Thank you.