KISS.HttpClientAuthentication 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package KISS.HttpClientAuthentication --version 3.0.0
                    
NuGet\Install-Package KISS.HttpClientAuthentication -Version 3.0.0
                    
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="KISS.HttpClientAuthentication" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KISS.HttpClientAuthentication" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="KISS.HttpClientAuthentication" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KISS.HttpClientAuthentication --version 3.0.0
                    
#r "nuget: KISS.HttpClientAuthentication, 3.0.0"
                    
#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.
#:package KISS.HttpClientAuthentication@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=KISS.HttpClientAuthentication&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=KISS.HttpClientAuthentication&version=3.0.0
                    
Install as a Cake Tool

KISS.HttpClientAuthentication NuGet package

ABOUT

This NuGet package enables configuring authentication for HttpClients using the ASP.NET Core Configuration system.

The package currently supports the following authentication methods:

  • Api key
  • Basic
  • OAuth2
    • Client credentials

USAGE

Add the NuGet package KISS.HttpClientAuthentication to your project and whenever a AddHttpClient injection is done that needs authentication chain a call to AddAuthenticatedHttpMessageHandler for the AddHttpClient request and set the configuration accordingly.

There are two different AddAuthenticatedHttpMessageHandler extension methods. One without any parameter and one with a string configSection parameter. The first one will use the IHttpClientBuilder.Name as the name of the configuration to read from the ASP.NET Core Configuration system, while the latter gives you the opportunity to specify a specific configuration setting.

Configuration

Various authentication methods are supported by the following configuration settings.

None

No authentication.

"<Section name>": {
  "AuthenticationProvider": "None"
}
Api Key

Authentication using API key, both Header and Value must be set.

"<section name>": {
  "AuthenticationProvider": "ApiKey",
  "ApiKey": {
    "Header": "<API KEY HEADER>",
    "Value": "<API KEY VALUE>"
  }
}
Basic

Authentication using username/password authentication, both Username and Password must be set.

"<section name>": {
  "AuthenticationProvider": "Basic",
  "Basic": {
    "Username": "<username>",
    "Password": "<password>"
  }
}
OAuth 2

Authentication using OAuth2.

Client credentials

Using OAuth2 client credentials, all settings except DisableTokenCache, Scope and TokenEndpoint's Additional*Parameters is required.

"<section name>": {
  "AuthenticationProvider": "OAuth2",
  "OAuth2": {
    "DisableTokenCache": false,
    "GrantType": "ClientCredentials",
    "Scope": "<Optional scopes separated by space>",
    "TokenEndpoint": {
        "Url": "<OAuth2 token endpoint>",
        "AdditionalHeaderParameters": {
        },
        "AdditionalBodyParameters": {
        },
        "AdditionalQueryParameters": {
        }
    },
    "ClientCredentials": {
        "ClientId": "<Unique client id>",
        "ClientSecret": "<Secret connected to the client id>"
    }
  }
}

The Additional*Parameters configuration is dynamic, any configuration in these will be added to their respective parts of the request accordingly. Please note that the AdditionalQueryParameters will be url encoded.

Examples

Example 1 - Uses the name of the type that is used in AddHttpClient

MyClass.cs

public class MyClass
{
}
// When configuring Dependency Injection
services.AddHttpClient<MyClass>().AddAuthenticatedHttpMessageHandler();

appsettings.json

{
    "MyClass": {
        // This configuration is used since the class name is MyClass
    }
}
Example 2 - Uses the configSection specified when calling AddAuthenticatedHttpMessageHandler

MyClass.cs

public class MyClass
{
}
// When configuring Dependency Injection
services.AddHttpClient<MyClass>().AddAuthenticatedHttpMessageHandler("MyConfiguration");

appsettings.json

{
    "MyConfiguration": {
        // This configuration is used since the HttpClient Name is MyConfiguration
    }
}
Example 3 - Uses the name specified when calling AddHttpClient
// When configuring Dependency Injection
services.AddHttpClient("MyClient").AddAuthenticatedHttpMessageHandler();

appsettings.json

{
    "MyClient": {
        // This configuration is used since the configSection is set to MyConfiguration
    }
}
Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 247 11/25/2025
3.0.0 808 1/14/2025
3.0.0-preview.250114.1 77 1/14/2025
2.0.2 405 10/15/2024
2.0.1 235 10/9/2024
2.0.0 379 6/4/2024
2.0.0-preview.240604.1 99 6/4/2024
1.0.0 225 9/1/2023