Skip to content

Instantly share code, notes, and snippets.

@PatrickDinh
Created February 9, 2020 06:03
Show Gist options
  • Select an option

  • Save PatrickDinh/133269793eafffba0d2389a690597e06 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickDinh/133269793eafffba0d2389a690597e06 to your computer and use it in GitHub Desktop.
Add JTW to HTTPClient with AzureServiceTokenProvider
services.AddHttpClient(WeatherApiHttpClientName, (provider, httpClient) =>
{
var appSettings = provider.GetService<IOptions<AppSettings>>().Value;
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessToken = azureServiceTokenProvider.GetAccessTokenAsync(appSettings.AdAppId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment