title: How to get HttpClient to pass credentials along with the request?
body: <p>I have a web application (hosted in IIS) that talks to a Windows service. The Windows service is using the ASP.Net MVC Web API (self-hosted), and so can be communicated with over http using JSON. The web application is configured to do impersonation, the idea being that the user who makes the request to the web application should be the user that the web application uses to make the request to the service. The structure looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/EQonG.png" alt=""></p>
  
  <p><sub>(The user highlighted in red is the user being referred to in the examples below.)</sub></p>
  
  <hr>
  
  <p>The web application makes requests to the Windows service using an <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx"><code>HttpClient</code></a>:</p>
  
  <pre><code>var httpClient = new HttpClient(new HttpClientHandler() 
                        {
                            UseDefaultCredentials = true
                        });
  httpClient.GetStringAsync("http://localhost/some/endpoint/");
  </code></pre>
  
  <p>This makes the request to the Windows service, but does not pass the credentials over correctly (the service reports the user as <code>IIS APPPOOL\ASP.NET 4.0</code>). <strong>This is not what I want to happen</strong>.</p>
  
  <p>If I change the above code to use a <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.100%29.aspx"><code>WebClient</code></a> instead, the credentials of the user are passed correctly:</p>
  
  <pre><code>WebClient c = new WebClient
                     {
                         UseDefaultCredentials = true
                     };
  c.DownloadStringAsync(new Uri("http://localhost/some/endpoint/"));
  </code></pre>
  
  <p>With the above code, the service reports the user as the user who made the request to the web application.</p>
  
  <p><strong>What am I doing wrong with the <code>HttpClient</code> implementation that is causing it to not pass the credentials correctly</strong> (or is it a bug with the <code>HttpClient</code>)?</p>
  
  <p><sub>The reason I want to use the <code>HttpClient</code> is that it has an async API that works well with <code>Task</code>s, whereas the <code>WebClient</code>'s asyc API needs to be handled with events.</sub></p>