Keycloak.Net.Authentication 2.1.0

dotnet add package Keycloak.Net.Authentication --version 2.1.0
                    
NuGet\Install-Package Keycloak.Net.Authentication -Version 2.1.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="Keycloak.Net.Authentication" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Keycloak.Net.Authentication" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Keycloak.Net.Authentication" />
                    
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 Keycloak.Net.Authentication --version 2.1.0
                    
#r "nuget: Keycloak.Net.Authentication, 2.1.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 Keycloak.Net.Authentication@2.1.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=Keycloak.Net.Authentication&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Keycloak.Net.Authentication&version=2.1.0
                    
Install as a Cake Tool

Keycloak .Net Authentication

Build Build

Authentication with Keycloak in .NET and ASP.NET Core. Secure your api with Keycloak JWT bearer authentication

Add the Keycloak.Net.Authentification nuget package to your project.

Api calls requires auhorization header with an JWT token from Keycloak.

POST https://yourapi/action HTTP/1.1
Auhorization: Bearer JwtTokenContent

How to use

Add to program.cs of your api

Option no.1 (easiest way)
builder.Services
  .AddKeyCloakAuthentication()
  .AddKeyCloakJwtBearerOptions("appsettings_section_name");
.....
app.UseAuthentication();
app.UseAuthorization();

Add section to the appsettings.{Environment}.json

  • Authority is required. If not set will throw exception during app startup.
  • Set the audience as "Audience", "ValidAudience" or "ValidAudiences". If not set will throw exception during app startup.
 {
    "KeycloakUrl": "<<FROM_USER_SECRET>>",
    "RealmName": "<<FROM_USER_SECRET>>",
    
    "appsettings_section_name": {
          "Authority": "{KeycloakUrl}{RealmName}",
          "Audience": "<<Audience>>"
       }
// or
    "appsettings_section_name": {
          "Authority": "{KeycloakUrl}{RealmName}",
          "ValidAudience": "<<Audience>>"
       }
//or
    "appsettings_section_name": {
          "Authority": "{KeycloakUrl}{RealmName}",
          "ValidAudiences": ["<<Audience>>"]
       }
 }
Option no.2
  • Variation of first option. Additional configuration of JwtBearerOptions available.
  • If set will override the value of same property defined in appsettings.{Environment}.json
builder.Services
  .AddKeyCloakAuthentication()
  .AddKeyCloakJwtBearerOptions("appsettings_section_name", options =>
  {
    options.Audience = "<<Audience>>";
    options.SaveToken = true;

    options.TokenValidationParameters.ClockSkew = TimeSpan.FromSeconds(30);
  });
Option no.3
  • If you want to get auth options from other source, not from appsettings.{Environment}.json, you can pass Action<JwtBearerValidationOptions> instead.
  • Authority is required. If not set will throw exception during app startup.
  • Set the audience as "Audience", "ValidAudience" or "ValidAudiences".If not set will throw exception during app startup.
builder.Services
  .AddKeyCloakAuthentication()
  .AddKeyCloakJwtBearerOptions(options =>
    {
        options.KeycloakAuthority = "https://{host}/realms/{realm}";
        options.KeycloakAudience = "<<Audience>>";
    })

Option no.4
  • You have to manually configure the JwtBearerOtions.
builder.Services
  .AddKeyCloakAuthentication()
  .AddJwtBearerOptions(options =>
    {
        options.Authority = "https://{host}/realms/{realm}";
        options.Audience = "<<Audience>>";
        ......
        options.TokenValidationParameters = new TokenValidationParameters( options =>
        {
          options.ClockSkew = TimeSpan.FromSeconds(30);
          .......
        });
    });

JWT transformation

  • Under the hood the Keyclaok JWT is mapped and transformed to Identity JWT
  • The Keycloak Realm and Client "roles" claims are mapped and transformed to ClaimType.Role
  • By default the Keycloak "preferred_username" claim is transformed to ClaimType.Name. You can change it by just adding the following:
{
    "KeycloakUrl": "<<FROM_USER_SECRET>>",
    "RealmName": "<<FROM_USER_SECRET>>",
    
    "appsettings_section_name": {
          "Authority": "{KeycloakUrl}{RealmName}",
          "Audience": "<<Audience>>",
          "NameClaim: "<<NameOfClaimWhichShouldBeSetToNameClaim>>"
       }
}
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 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on Keycloak.Net.Authentication:

Package Downloads
Keycloak.Net.Authorization

Authorization with Keycloak in .NET and ASP.NET Core

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 147 11/1/2024
2.0.0 179 2/20/2024
1.0.3 159 1/27/2024
1.0.2 157 1/24/2024
1.0.2-rc0.2 72 1/23/2024
1.0.2-rc0.1 67 1/23/2024
1.0.1 148 1/15/2024
1.0.0-rc01 108 1/14/2024
1.0.0-rc 117 1/12/2024