question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When I use Audit.NET.WebApi in my Core 2.2 project is blanks out my request body.

See original GitHub issue

Middleware registration

            application
            .UseAuditMiddleware(_ => _
                .FilterByRequest(rq => !rq.Path.Value.EndsWith("favicon.ico"))
                .WithEventType("{verb}:{url}")
                .IncludeHeaders()
                .IncludeResponseHeaders()
                .IncludeRequestBody()
                .IncludeResponseBody());

MVC Builder Options

                    options.AddAuditFilter(config => config
                        .LogAllActions()
                        .WithEventType("{verb}.{controller}.{action}")
                        .IncludeHeaders(ctx => !ctx.ModelState.IsValid)
                        .IncludeRequestBody()
                        .IncludeModelState()
                        .IncludeResponseBody(ctx => ctx.HttpContext.Response.StatusCode == 200));

Response from MVC

{
  "errors": {
    "": [
      "A non-empty request body is required."
    ]
  },
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "0HLP5K21HO1PH:00000001"
}

Audit JSON file output during a request:

{"EventType":"POST.Entity.Post","Environment":{"UserName":"REDACTED","MachineName":"REDACTED","DomainName":"REDACTED","CallingMethodName":"MyApp.EntityCreation.Controllers.EntityController.Post()","AssemblyName":"MyApp.EntityCreation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","Culture":"en-US"},"StartDate":"2019-08-20T18:59:22.4398849","EndDate":"2019-08-20T18:59:22.5187417","Duration":79,"Action":{"TraceId":"0HLP5K21HO1PH:00000001","HttpMethod":"POST","ControllerName":"Entity","ActionName":"Post","ActionParameters":{"cancellationToken":{"IsCancellationRequested":false,"CanBeCanceled":true,"WaitHandle":{"Handle":{"value":1668},"SafeWaitHandle":{"IsInvalid":false,"IsClosed":false}}}},"RequestUrl":"http://localhost/Entity","IpAddress":"::1","ResponseStatus":"Bad Request","ResponseStatusCode":400,"RequestBody":{"Type":"application/json","Length":171,"Value":"{\n\t\"FirstName\": \"dfg\",\n\t\"LastName\": \"dfg\",\n\t\"MiddleName\": \"fsdfg\",\n\t\"PreferredFirstName\": \"\",\n\t\"EmailAddress\": \"\",\n\t\"EmployeeId\": \"\",\n\t\"StudentId\": \"\",\n\t\"WustlEduId\": \"\"\n}"},"ResponseBody":{"Type":"BadRequestObjectResult","Value":{"errors":{"":["A non-empty request body is required."]},"title":"One or more validation errors occurred.","status":400,"traceId":"0HLP5K21HO1PH:00000001"}},"Headers":{"Content-Type":"application/json","Accept":"*/*","Authorization":"Basic Sm9objpEb2U=","Host":"localhost","User-Agent":"insomnia/6.6.2","Content-Length":"171"},"ResponseHeaders":{"Content-Type":"application/problem+json; charset=utf-8"},"ModelStateValid":false,"ModelStateErrors":{"":"A non-empty request body is required."}}}

Simply commenting out the UseAuditMiddleware solves the problem and the exact same request succeeds without issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
thepirat000commented, Aug 20, 2019

Did you see this note?

When using IncludeRequestBody you have to make sure to enable rewind on the request body stream, otherwise the controller won’t be able to read the request body since, by default, it’s a forwand-only stream that can be read only once. You can enable rewind on your startup logic with the following code:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Use(async (context, next) => {  // <----
        context.Request.EnableBuffering(); // or .EnableRewind();
        await next();
    });
    
    app.UseMvc();
}
0reactions
VictorioBerracommented, Oct 9, 2019

Upon further reading, I think I misunderstood what the new feature was. We can probably disregard.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Request body from is empty in .Net Core 3.0
I am using the extension in my startup to add Newtonsoft to my project for .Net Core 3.0, but if I remove that...
Read more >
How to Audit Your ASP.NET Core WebApi
In this post, I'm gonna tell you how you can have proper audit log without re-inventing the wheel! I picked up a task...
Read more >
ASP.NET Core 3.1 API - JWT Authentication with Refresh ...
The Token property is optional in the request body because the route also supports revoking the token sent in the refreshToken cookie. If...
Read more >
ASP.NET Core 3.1 - Boilerplate API with Email Sign Up, ...
How to build a boilerplate authentication API with ASP.NET Core 3.1 that includes email sign up & verification, authentication & role based ...
Read more >
Untitled
Peak to peak amplitude vibration, Web api mvc vs wcf, Dr. bimmler, Tufeyl kimdir, ... The intersector project, Br 3 chapter 40, Friendship...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found