title: NetWorkCredentials not showing up in headers
body: <p>I have two projects: a Web API project and a client project.</p>
  <p>In the client application, I configure my <code>HttpClient</code> like this.</p>
  <pre><code>services.AddHttpClient&lt;TrackAndTraceClient&gt;()
      .ConfigureHttpClient(httpClient =&gt;
      {
          httpClient.BaseAddress = new Uri(settings.BaseUrl);
          httpClient.Timeout = TimeSpan.FromMinutes(5);
      })
      .ConfigurePrimaryHttpMessageHandler(serviceProvider =&gt;
      {
          return new HttpClientHandler()
          {
              Credentials = new NetworkCredential(settings.Username, settings.Password),
          };
      });
  </code></pre>
  <p>And then in my class that calls the API:</p>
  <pre><code>public TrackAndTraceClient(IHttpClientFactory httpClientFactory, IOptions&lt;TrackAndTraceSettings&gt; settings)
  {
      HttpClient = httpClientFactory.CreateClient(nameof(TrackAndTraceClient));
      Settings = settings.Value;
  }
  </code></pre>
  <p>My Web API site implements basic authentication using the techniques described in <a href="https://www.roundthecode.com/dotnet/how-to-add-basic-authentication-to-asp-net-core-application" rel="nofollow noreferrer">this article</a>. But my code throws an exception because no <em>Authorization</em> header is found.</p>
  <pre><code>public class BasicAuthenticationHandler : AuthenticationHandler&lt;AuthenticationSchemeOptions&gt;
  {
      protected override async Task&lt;AuthenticateResult&gt; HandleAuthenticateAsync()
      {
  
          // ...
  
          if (!Request.Headers.TryGetValue(&quot;Authorization&quot;, out StringValues authHeaderValues))
              throw new Exception(&quot;Missing Authorization header&quot;);
  
          // ...
      }
  }
  </code></pre>
  <p>I can only get this to work by adding the following code to my class that calls the API:</p>
  <pre><code>HttpClient.DefaultRequestHeaders.Add(&quot;ContentType&quot;, &quot;application/json&quot;);
  byte[] credentialsData = Encoding.UTF8.GetBytes($&quot;{Settings.Username}:{Settings.Password}&quot;);
  string credentials = Convert.ToBase64String(credentialsData);
  HttpClient.DefaultRequestHeaders.Add(&quot;Authorization&quot;, $&quot;Basic {credentials}&quot;);
  </code></pre>
  <p>Can anyone tell me why this last block of code is needed? Why doesn't setting the credentials with <code>NetworkCredential</code> appear to do anything? And how can I change my Web API so that it works with credentials specified the original way?</p>
  <p>Note that I'm also calling a third-party API, and the client is configured exactly the same way as in my first block of code. So I know that can be made to work.</p>