title: Configure HttpClient to use a service to populate the authentication header?
body: <p>I want to use a <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1#typed-clients" rel="nofollow noreferrer">typed HTTP client</a> so I register it like this in the <code>ServiceCollection</code></p>
  <pre><code>services.AddHttpClient&lt;ApiService&gt;();
  </code></pre>
  <p>All the requests that the client makes, must be authenticated. So I'd need to slightly modify the snippet above to this:</p>
  <pre><code>services.AddHttpClient&lt;ApiService&gt;(c =&gt;
  {
      c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, &quot;&quot;);
  });
  </code></pre>
  <p>The bearer token needs to be renewed all 5 minutes, so I need to create service that needs dependencies like an HttpClient (to authenticate and get the token) and a MemoryCache (to store the token as long it's valid).</p>
  <p>The problem is, as you can see, this is still in the phase before the service provider is built, so I can't resolve anything at this point... What are my options here?</p>