Skip to content

Instantly share code, notes, and snippets.

@RickyLin
Created April 17, 2021 08:40
Show Gist options
  • Save RickyLin/0b0db24edb552cc6a0aaabb2926d6109 to your computer and use it in GitHub Desktop.
Save RickyLin/0b0db24edb552cc6a0aaabb2926d6109 to your computer and use it in GitHub Desktop.
Access token management for HttpClient
// add IdentityModel.AspNetCore package
using Microsoft.Extensions.DependencyInjection;
using IdentityModel.Client;
public class Startup
{
/* removed all other code for brevity */
public void ConfigureServices(IServiceCollection services)
{
string tokenClientName = "DemoClient";
services.AddAccessTokenManagement(options =>
{
options.Client.Clients.Add(tokenClientName, new ClientCredentialsTokenRequest
{
Address = "https://[authority]/connect/token",
ClientId = "clientId",
ClientSecret = "Client Secret",
Scope = "Scope"
});
});
services.AddHttpClient<MyDemoHttpClient>().AddClientAccessTokenHandler(tokenClientName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment