title: Post data to Web Api 2 using HttpClient with Authentication
body: <p>I working on a <code>ASP.Net MVC 5</code> project which recently required to use <code>Web API</code> to send some data via <code>POST</code> to another server.</p>
  
  <p>It's more like a middle <code>API</code> which get some data via <code>POST</code> and send it to another server.
  This <code>API</code> require Authentication with <code>username</code> and <code>password</code>.</p>
  
  <p>The default Authentication in this project is <code>ASP.Net Identity 2</code>.</p>
  
  <p>Now my problem is that the authentication I am passing with <code>HttpClient</code> does not work and return <code>status code 200</code> and login path.</p>
  
  <p>Here is my API controller:</p>
  
  <pre><code>public class SendController : ApiController
  {
      [Authorize]
      [EnableCors(origins: "*", headers: "*", methods: "*")]
      [Route("~/api/send/data")]
      public IHttpActionResult PostData(Product data)
      {
          return Ok("done.");
      }
  } 
  </code></pre>
  
  <p>And the code i am using:</p>
  
  <pre><code>using (var client = new HttpClient())
          {
              client.BaseAddress = new Uri("http://localhost:2494/");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              client.DefaultRequestHeaders.Authorization =
                  new AuthenticationHeaderValue(
                      "Basic",
                      Convert.ToBase64String(
                          System.Text.ASCIIEncoding.ASCII.GetBytes(
                              string.Format("{0}:{1}", "kamal", "123456"))));
  
              // HTTP GET
              HttpResponseMessage response = await client.GetAsync("api/send");
  
              // HTTP POST
              var gizmo = new Product() { Name = "Gizmo", Price = 100, Category = "Widget" };
              response = await client.PostAsJsonAsync("api/send/data", gizmo);
              if (response.IsSuccessStatusCode)
              {
                  // 
              }
          }
  </code></pre>
  
  <p>This is the response message:</p>
  
  <pre><code>{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
  {
    X-Frame-Options: SAMEORIGIN
    X-SourceFiles: =?UTF-8?B?RDpcd29ya1xfXFNtc0FwaVxTbXNBcGkuV2ViXGFjY291bnRcbG9naW4=?=
    Cache-Control: private
    Date: Fri, 24 Jun 2016 03:04:32 GMT
    Set-Cookie: __RequestVerificationToken=D0tzqAqeERwp2EPr9V0_MYGn0syYsoBeBTS6C28sjPVo436KO837RC_iXZQstD3bqrH5EGKry7gHCFoqvg8ZNpDVDgA1; path=/; HttpOnly
    X-Powered-By: ASP.NET
    Content-Length: 2700
    Content-Type: text/html; charset=utf-8
  }}
  </code></pre>
  
  <p>And the request message:</p>
  
  <pre><code>{Method: POST, RequestUri: 'http://localhost:2494/Account/Login?ReturnUrl=%2Fapi%2Fsend%2Fdata', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[SmsApi.Web.Models.Product, SmsApi.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Headers:
  {
    Accept: application/json
    Authorization: Basic a2FtYWw6MTIzNDU2Nzg5MDA=
    Content-Type: application/json; charset=utf-8
    Content-Length: 50
  }}
  </code></pre>
  
  <p>Is there some configuration i need to set in my project? </p>
  
  <p>Also there is something makes me confused, if i remove <code>Authorize</code> attribute, the post request will work but it's also need to send a get request. i mean if i comment the get request, the post request will return <code>Methos Not Allow</code>. Why?</p>