title: Custom DelegatingHandler never return after await base.SendAsync
body: <p>I'm building an API consumer using HttpClient. Because the provider require the consumer to authenticate using Digest Authentication, so I need to write a custom DelegatingHandler like below:</p>
  
  <pre><code>public class DigestAuthDelegatingHandler : DelegatingHandler
  {
      public DigestAuthDelegatingHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }
      protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          var response = await base.SendAsync(request, cancellationToken);
          if (!response.IsSuccessStatusCode &amp;&amp; response.StatusCode == HttpStatusCode.Unauthorized)//This line of code is never reached
          {
              //Generate the Digest Authorization header string and add to the request header,
              //then try to resend the request to the API provider
          }
          return response;
      }
  }
  </code></pre>
  
  <p>I create a HttpClient and add my custom DelegatingHandler to the message handlers pineline</p>
  
  <pre><code>HttpClient httpClient = new HttpClient(new DigestAuthDelegatingHandler(new HttpClientHandler()));            
  httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  httpClient.BaseAddress = new Uri("http://127.0.0.1/");
  HttpResponseMessage response = httpClient.GetAsync("api/getTransactionInfo?TransactionNumber=1000).Result;   
  </code></pre>
  
  <p>After doing that, it look like that my consumer runs forever. When I add a break point AFTER the code line await base.SendAsync above, I see that the code will never return, so I have no way to check if the response is get an 401 unauthorized to extract the Digest authorization header details. Nothing wrong at the API provider because I've successfully built another API consumer site using the traditional WebHttpRequest support Digest Authenticate and it works well.</p>
  
  <p>IMPORTANT NOTE: if I switch to write consumer as a Console Application then it works well. So, I'm not sure but I think it's problem related to ASP.NET thread when running in asynchronous mode?</p>
  
  <p>Is there anything wrong I'm doing?</p>