title: Access Raw Request Body
body: <p>I'm trying to access a request's raw input body/stream in ASP.net 5. In the past, I was able to reset the position of the input stream to 0 and read it into a memory stream but when I attempt to do this from the context the input stream is either null or throws an error (System.NotSupportedException => "Specified method is not supported.").</p>
  
  <p>In the first example below I can access the raw request in a controller if I declare the controller method's parameter object type as dynamic. For various reasons, this is not a solution and I need to access the raw request body in an authentication filter anyways.</p>
  
  <p>This Example Works, But Is Not a Reasonable Solution:</p>
  
  <pre><code>[HttpPost("requestme")]
  public string GetRequestBody([FromBody] dynamic body)
  {   
      return body.ToString();
  }
  </code></pre>
  
  <p>Throws Error:</p>
  
  <pre><code>[HttpPost("requestme")]
  public string GetRequestBody()
  {
      var m = new MemoryStream();
      Request.Body.CopyTo(m);
  
      var contentLength = m.Length;
  
      var b = System.Text.Encoding.UTF8.GetString(m.ToArray());
  
      return b;
  }
  </code></pre>
  
  <p>Throws Error:</p>
  
  <pre><code>[HttpPost("requestme")]
  public string GetRequestBody()
  {
      Request.Body.Position = 0;
      var input = new StreamReader(Request.Body).ReadToEnd();
  
      return input;
  }
  </code></pre>
  
  <p>Throws Error:</p>
  
  <pre><code>[HttpPost("requestme")]
  public string GetRequestBody()
  {
      Request.Body.Position = 0;
      var input = new MemoryStream();
      Request.Body.CopyTo(input);
  
      var inputString = System.Text.Encoding.UTF8.GetString(input.ToArray());
  
      return inputString;
  }
  </code></pre>
  
  <p>I need to access the raw request body of every request that comes in for an API that I am building.</p>
  
  <p>Any help or direction would be greatly appreciated!</p>
  
  <p>EDIT:</p>
  
  <p>Here is the code that I would like to read the request body in.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNet.Mvc;
  using Microsoft.AspNet.Http;
  
  namespace API.Filters
  {
      public class CustomAuthorizationAttribute : Attribute, IAuthorizationFilter
      {
          public CustomAuthorizationAttribute()
          { }
  
          public void OnAuthorization(AuthorizationContext context)
          {
              if (context == null)
                  throw new ArgumentNullException("OnAuthorization AuthorizationContext context can not be null.");
              else
              {
                  if (this.AuthorizeCore(context.HttpContext) == false)
                  {
                      // Do Other Stuff To Check Auth
                  }
                  else
                  {
                      context.Result = new HttpUnauthorizedResult();
                  }
              }
          }
  
          protected virtual bool AuthorizeCore(HttpContext httpContext)
          {
              var result = false;
  
              using (System.IO.MemoryStream m = new System.IO.MemoryStream())
              {
                  try
                  {
                      if (httpContext.Request.Body.CanSeek == true)
                          httpContext.Request.Body.Position = 0;
  
                      httpContext.Request.Body.CopyTo(m);
  
                      var bodyString = System.Text.Encoding.UTF8.GetString(m.ToArray());
  
                      return CheckBody(bodyString); // Initial Auth Check returns true/false &lt;-- Not Shown In Code Here on Stack Overflow
                  }
                  catch (Exception ex)
                  {
                      Logger.WriteLine(ex.Message);
                  }
              }
                  return false;
          }
      }
  }
  </code></pre>
  
  <p>This code would be accessed when a call is made to a controller method marked with the CustomAuthorization attribute like so.</p>
  
  <pre><code>[Filters.CustomAuthorizationAuthorization]
  [HttpPost]
  public ActionResult Post([FromBody]UserModel Profile)
  {
      // Process Profile
  }
  </code></pre>