Tingle.Extensions.Http.Authentication 4.14.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tingle.Extensions.Http.Authentication --version 4.14.1                
NuGet\Install-Package Tingle.Extensions.Http.Authentication -Version 4.14.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tingle.Extensions.Http.Authentication" Version="4.14.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tingle.Extensions.Http.Authentication --version 4.14.1                
#r "nuget: Tingle.Extensions.Http.Authentication, 4.14.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tingle.Extensions.Http.Authentication as a Cake Addin
#addin nuget:?package=Tingle.Extensions.Http.Authentication&version=4.14.1

// Install Tingle.Extensions.Http.Authentication as a Cake Tool
#tool nuget:?package=Tingle.Extensions.Http.Authentication&version=4.14.1                

Tingle.Extensions.Http.Authentication

This library adds support for custom authentication via the Authorization header when using HttpClient. In some cases, setting the header once is not sufficient. Instead you may want to refresh the token only after it expires. This functionality builds upon the DelegatingHandler and hence can be used via DI using IHttpClientBuilder or without by wrapping inner handlers.

Also see sample.

The supported authentication patterns:

API Key in the Authorization header

Example:

builder.Services.AddHttpClient<MyCustomClient>()
                .AddApiKeyHeaderAuthenticationHandler("my-api-key-here", scheme: "Bearer");

This will add an Authorization header → Authorization: Bearer my-api-key-here

API Key in the query string

Example:

builder.Services.AddHttpClient<MyCustomClient>()
                .AddApiKeyQueryAuthenticationHandler("my-api-key-here", queryParameterName: "key");

This will append to the query string of the request before the request is sent out. E.g. https://contoso.com/?key=my-api-key-here

Pre-Shared Key (PSK) in the Authorization header

This behaves similar to Microsoft's shared key authentication which you can also use in your own projects. Every request ends up with a different authorization value and can be safer in some situations compared to using OAuth.

builder.Services.AddHttpClient($"{nameof(Worker)}4")
                .AddSharedKeyAuthenticationHandler("my-base-64-encoded-key", scheme: "Bearer");

This will add an Authorization here → Authorization: Bearer {base64-request-hash-will-be-set-here}

OAuth Client Credentials

This follows the OAuth 2.0 client_credentials flow and optionally caches access tokens using IMemoryCache or IDistributedCache for the duration it is valid.

Example:

builder.Services.AddMemoryCache();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddHttpClient<MyCustomClient>()
                .AddAuthenticationHandler(provider => new OAuthClientCredentialHandler()
                {
                    Scheme = "Bearer",

                    // set OAuth values to match your scenario
                    AuthenticationEndpoint = "https://oauth-1.contoso.com",
                    Resource = "https://api.contoso.com",
                    ClientId = "awesome-app-id",
                    ClientSecret = "super-secret",

                    Logger = provider.GetRequiredService<ILogger<Program>>(), // optional, useful for debugging

                    // caching can be disabled by setting either CacheKey or Cache to null
                    CacheKey = $"{nameof(MyCustomClient)}:auth-token",
                    // either IMemoryCache or IDistributedCache
                    Cache = new(provider.GetRequiredService<Microsoft.Extensions.Caching.Memory.IMemoryCache>()),
                    //Cache = new(provider.GetRequiredService<Microsoft.Extensions.Caching.Distributed.IDistributedCache>()),
                });

Azure B2B via OAuth Client Credentials

Works like OAuth Client Credentials with a slight customization for Azure AD by setting the AuthorizationEndpoint based on the value supplied for TenantId.

Example:

builder.Services.AddMemoryCache();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddHttpClient<MyCustomClient>()
                .AddAuthenticationHandler(provider => new AzureAdB2BHandler
                {
                    Scheme = "Bearer",

                    // set OAuth values to match your scenario
                    TenantId = "00000000-0000-1111-0001-000000000000",
                    Resource = "https://api.contoso.com",
                    ClientId = "awesome-app-id",
                    ClientSecret = "super-secret",

                    Logger = provider.GetRequiredService<ILogger<Program>>(), // optional, useful for debugging

                    // caching can be disabled by setting either CacheKey or Cache to null
                    CacheKey = $"{nameof(MyCustomClient)}:auth-token",
                    // either IMemoryCache or IDistributedCache
                    Cache = new(provider.GetRequiredService<Microsoft.Extensions.Caching.Memory.IMemoryCache>()),
                    //Cache = new(provider.GetRequiredService<Microsoft.Extensions.Caching.Distributed.IDistributedCache>()),
                });
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tingle.Extensions.Http.Authentication:

Package Downloads
Tingle.Extensions.PushNotifications

Clients for sending push notifications via FCM, APNS etc

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 99 11/19/2024
4.14.1 337 10/14/2024
4.14.0 403 9/16/2024
4.13.0 650 8/13/2024
4.12.0 202 8/7/2024
4.11.2 366 7/15/2024
4.11.1 395 6/26/2024
4.11.0 328 6/6/2024
4.10.1 121 6/5/2024
4.10.0 216 5/27/2024
4.9.0 357 5/16/2024
4.8.0 392 5/5/2024
4.7.0 599 3/25/2024
4.6.0 451 3/8/2024
4.5.0 2,149 11/22/2023
4.4.1 332 11/20/2023
4.4.0 259 11/15/2023
4.3.0 553 10/18/2023
4.2.2 1,116 9/20/2023
4.2.1 1,061 8/4/2023
4.2.0 973 5/31/2023
4.1.1 274 5/26/2023
4.1.0 368 5/22/2023
4.0.0 1,489 3/14/2023
3.8.1 1,568 11/30/2022
3.8.0 1,041 11/21/2022
3.7.0 2,864 9/4/2022
3.6.0 2,429 7/4/2022
3.5.3 1,997 5/16/2022