A secure, resilient HTTP client registration and access token management library for .NET and Blazor. Easily add OAuth2-protected HttpClients to your application with support for both Client Credentials and Authorization Code PKCE flows.
- Register HttpClients that automatically acquire and attach OAuth2 access tokens
- Supports Client Credentials and Authorization Code PKCE flows
- Built-in token caching and refresh
- Pluggable token providers via
IAccessTokenProvider - Resilience (retry with jitter) for HTTP requests
- Extension methods for adding bearer tokens to requests
- Simple integration with Blazor, ASP.NET Core, and .NET DI
Install via NuGet:
dotnet add package Goodtocode.SecuredHttpClient
services.AddClientCredentialHttpClient(
configuration, // IConfiguration
clientName: "MyApiClient",
baseAddress: new Uri("https://api.example.com"),
maxRetry: 5 // optional
);services.AddAuthCodePkceHttpClient(
configuration, // IConfiguration
clientName: "MyApiClient",
baseAddress: new Uri("https://api.example.com"),
maxRetry: 5 // optional
);public static IServiceCollection AddAccessTokenHttpClient(
this IServiceCollection services,
Action<ResilientHttpClientOptions> configureOptions)
{
var options = new ResilientHttpClientOptions();
configureOptions(options);
if (options.BaseAddress == null)
throw new ArgumentNullException(nameof(configureOptions), "BaseAddress must be provided.");
if (string.IsNullOrWhiteSpace(options.ClientName))
throw new ArgumentNullException(nameof(configureOptions), "ClientName must be provided.");
services.AddOptions<AuthCodePkceOptions>()
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddScoped<IAccessTokenProvider, DownstreamApiAccessTokenProvider>();
services.AddScoped<TokenHandler>();
services.AddHttpClient(options.ClientName, clientOptions =>
{
clientOptions.DefaultRequestHeaders.Clear();
clientOptions.BaseAddress = options.BaseAddress;
})
.AddHttpMessageHandler<TokenHandler>()
.AddStandardResilienceHandler(resilienceOptions =>
{
resilienceOptions.Retry.UseJitter = true;
resilienceOptions.Retry.MaxRetryAttempts = options.MaxRetry;
});
return services;
}using Goodtocode.SecuredHttpClient.Extensions;
httpClient.AddBearerToken("your-access-token");ClientCredentialOptions: For client credentials flow (ClientId, ClientSecret, TokenUrl, Scope)AuthCodePkceOptions: For PKCE flow (ClientId, CodeVerifier, RedirectUri, TokenUrl, Scope)ResilientHttpClientOptions: For base address, client name, and retry settings
- Token providers implement
IAccessTokenProviderand handle token acquisition and caching TokenHandlerautomatically attaches the access token to outgoing requests- Resilience is provided via retry policies with jitter
MIT
| Version | Date | Changes |
|---|---|---|
| 1.1.0 | 2026-01-22 | Bump from .NET 9 to .NET 10 |