CoreWCF / CoreWCF/CoreWCF

[Question] : Read request body from HTTPContext when MessageSecurityException thrown

Open
#1,248 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
1.8k
Forks
320
PR merge metrics
No merged PRs in 30d

Description

### Duplicate ?

- [X] I have searched issues/discussions and did not find other issues/discussions reporting this bug.

### Product version

1.3.1

### Describe expected behavior

Hello,

I am trying to read the request body from HTTPContext when a MessageSecurityException is thrown for logging it. I have tried reading the stream but I receive an Exception.

I have implemented a middleware that catches exception, and try to read the stream.

How can I read it?

Can anyone help me please?

Thank you! 🙂

### Describe actual behavior

Read the body

### Which binding

BasicHttp

### security

TransportCredentialOnly

### Which .NET version

.NET 7

### Which os platform

Linux

### Code snippet used to reproduce the issue

```c#
`public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (CoreWCF.Security.MessageSecurityException)
{
Random random = new();
if (_options.CurrentValue.Enabled && random.Next(0, 101) <= _options.CurrentValue.Percent)
{
using (StreamReader streamReader = new(context.Request.Body))
{
string rq = await streamReader.ReadToEndAsync();
_logger.LogError($"MessageSequrityException for RQ: {rq}");
}
};
throw;
}
catch (Exception)
{
throw;
}
}`
```

### Stacktrace if any

```shell
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(System.Memory, System.Threading.CancellationToken) en HttpRequestStream.cs
System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder.StateMachineBox.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(short) en PoolingAsyncValueTaskMethodBuilderT.cs
System.IO.StreamReader.ReadBufferAsync(System.Threading.CancellationToken) en StreamReader.cs
System.IO.StreamReader.ReadToEndAsyncInternal(System.Threading.CancellationToken) en StreamReader.cs
Front.Core.App.Middlewares.RequestExceptionAuditerMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext) en RequestExceptionAuditerMiddleware.cs
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(Microsoft.AspNetCore.Http.HttpContext) en DeveloperExceptionPageMiddlewareImpl.cs
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.