I'm trying to configure HttpClientFactory to use an Authorization header. The problem is that the auth token has to be retrieved from an auth service, which is another HTTP call that better be done with await. However, this doesn't work as expected (at least not in ASP.NET Core 2.2):
// Func<IServiceProvider, Task<string>> tokenFactory = ...
builder.ConfigureHttpClient(async (provider, httpClient) =>
{
string token = await tokenFactory(provider);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
});
It looks like whenever the action passed to ConfigureHttpClient is involved, it's not awaited.
Any ideas on workarounds? Should ConfigureHttpClient be improved or I'm missing some existing functionality that would allow doing that?