title: How to access/handle IOwinRequest Body
body: <p>I am writing a custom Owin middleware AuthenticationHandler for Hmac authentication.</p>
  
  <p>In the overriden 'AuthenticateCoreAsync' method I need to read the http headers and the 'Body' (content) of the request.</p>
  
  <p>I have everything working, claims / authentication ticket, etc.</p>
  
  <p>But whenever I access or do something with IOwinRequest.Body my WebApi controllers break. They are called correctly, but all api method arguments are 'null'. As if the body content gets lost in the process somewhere.</p>
  
  <p>I tried copying IOwinRequest.Body to a memory stream and use that, I tried resetting it using IOwinRequest.Body.Seek(0), etc. all to no avail.</p>
  
  <p>I can't find any good example / documentation on how to work with the Body of IOwinRequest either.</p>
  
  <p>Anyone can point me in the right direction?</p>
  
  <p>In the code below, it works when I leave it commented. When I uncomment those lines to actually do authentication (and read from Body), my webapi methods receive all null arguments.</p>
  
  <pre><code>internal class HMacAuthenticationMiddleware : AuthenticationMiddleware&lt;HMacAuthenticationOptions&gt;
  {
      private readonly ILogger logger;
  
      public HMacAuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, HMacAuthenticationOptions options)
          : base(next, options)
      {
          this.logger = app.CreateLogger&lt;HMacAuthenticationMiddleware&gt;();
      }
  
      protected override AuthenticationHandler&lt;HMacAuthenticationOptions&gt; CreateHandler()
      {
          return new HMacAuthenticationHandler(this.logger);
      }
  }
  </code></pre>
  
  <p>HMacAuthenticationHandler:</p>
  
  <pre><code>    protected override async Task&lt;AuthenticationTicket&gt; AuthenticateCoreAsync()
      {
          string[] apiKeys = null;
          AuthenticationTicket authenticationTicket = null;
          if (this.Request.Headers.TryGetValue(Configuration.ApiKeyHeader, out apiKeys))
          {
              ////var bufferedStream = await CreateStreamBuffer(Request);
              //var memStream = new MemoryStream();
              //await new StreamContent(Request.Body).CopyToAsync(memStream);
              //Request.Body = memStream;
              //memStream.Seek(0, SeekOrigin.Begin);
              //var httpMessage = Request.CreateRequestMessage(new StreamContent(memStream));
              //var authHandler = new HMacInnerAuthenticationHandler(httpMessage, Options);
              //var isAuthenticated = await authHandler.IsAuthenticated();
              //Request.Body.Seek(0, SeekOrigin.Begin);
  
              var isAuthenticated = true;
              if (isAuthenticated)
              {
                  var userName = this.Options.UsernameLookup.GetByApiKey(apiKeys.First());
                  var identity = new ClaimsIdentity(
                      new[]
                          {
                              new Claim(ClaimTypes.NameIdentifier, apiKeys.First(), XmlSchemaString, this.Options.AuthenticationType),
                              new Claim(ClaimTypes.Name, userName, XmlSchemaString, this.Options.AuthenticationType)
                          },
                      this.Options.AuthenticationType,
                      ClaimsIdentity.DefaultNameClaimType,
                      ClaimsIdentity.DefaultRoleClaimType);
  
                  authenticationTicket = new AuthenticationTicket(identity, null);
              }
          }
          return authenticationTicket;
      }
  </code></pre>