Request Body is null after Authorization

24 Views Asked by At

I have a situation in .Net 6 where I need to read the Request Body in the Authorization Middleware, then apparently the Request Body is null when it hits the controller method. In the controller method is have a dto being passed in with the [FromBody] attribute:

Controller:

  public IActionResult CreateCommittee([FromServices] ICommitteeService service, [Required, FromBody] CreateCommitteeDto createCommitteeDto)
  {
...
  }

Middleware:

private async Task<string> GetPartitionKeyAsync(HttpContext context)
 {
     string pk = "";
     pk = context.Request.Query["partitionKey"] + "";

     if (pk.Length == 0)
     {
         var req = context.Request;

         req.EnableBuffering();

         var body = context.Request.Body;
         StreamReader reader = new StreamReader(context.Request.Body);

             var json = await reader.ReadToEndAsync();
             var jsonObject = JObject.Parse(json);
             pk = jsonObject["partitionKey"].ToString();
             context.Request.Body.Position = 0;
  
     }
     return pk;
 }
0

There are 0 best solutions below