Created
April 17, 2021 08:40
-
-
Save RickyLin/0b0db24edb552cc6a0aaabb2926d6109 to your computer and use it in GitHub Desktop.
Access token management for HttpClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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