Skip to content

A HttpClient DelegatingHandler that handles OAuth handschake

License

Notifications You must be signed in to change notification settings

droosma/OAuth.HttpClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

edcdf92 · Sep 4, 2024

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OAuth.HttpClient

A DelegatingHandler implementation that abstracts away some of the authentication handshake logic like requesting the jwt token caching, and refreshing it.

NuGet version (ResilientOAuth.HttpClient)

Usage

    public void ConfigureServices(IServiceCollection services)
    {
        var settings = new Settings(clientId: "id",
                                    clientSecret: "secret",
                                    scope: "role:app",
                                    endpoint: new Uri("https://authority.local"),
                                    expireMargin: TimeSpan.FromMinutes(10));
        services.AddHttpClient("authenticatedHttpClient").WithOAuth(settings);
    }

Configuration

To facilitate configuration from IConfiguration you can use the SettingsDto to create the required settings directly from IConfiguration

Add the required settings to your appsettings.json

{
  "OAuthSettings": {
    "ClientId": "client_id",
    "ClientSecret": "client_secret",
    "Scopes": ["api1", "api2"],
    "TokenEndpoint": "https://localhost:5001/connect/token"
  }
}

Initialize the client with the correct settings (or any other ConfigurationSource)

var settings = hostBuilder.Configuration.GetRequiredSection("OAuthSettings").Get<SettingsDto>();
hostBuilder.Services.AddHttpClient("authenticatedHttpClient").WithOAuth(settings);