Keycloak.AuthServices.Common 2.0.0-pre-4

Keycloak.AuthServices

Build CodeQL NuGet contributionswelcome Conventional Commits License

Easy Authentication and Authorization with Keycloak in .NET and ASP.NET Core.

Package Version Description
Keycloak.AuthServices.Authentication Nuget Keycloak Authentication JWT + OICD
Keycloak.AuthServices.Authorization Nuget Authorization Services. Use Keycloak as authorization server
Keycloak.AuthServices.Sdk Nuget HTTP API integration with Keycloak

GitHub Actions Build History

Documentation

See the docs: https://nikiforovall.github.io/keycloak-authorization-services-dotnet

Getting Started

Install packages:

dotnet add package Keycloak.AuthServices.Authentication
dotnet add package Keycloak.AuthServices.Common
// Program.cs
using Keycloak.AuthServices.Authentication; 

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddKeycloakWebApiAuthentication(builder.Configuration); 
builder.Services.AddAuthorization(); 

var app = builder.Build();

app.UseAuthentication(); 
app.UseAuthorization(); 

app.MapGet("/", () => "Hello World!").RequireAuthorization(); 

app.Run();

In this example, configuration is based on appsettings.json.

//appsettings.json
{
    "Keycloak": {
        "realm": "Test",
        "auth-server-url": "http://localhost:8080/",
        "ssl-required": "none",
        "resource": "test-client",
        "verify-token-audience": false,
        "credentials": {
        "secret": ""
        },
        "confidential-port": 0
    }
}

Example Authorization

With Keycloak.AuthServices.Authorization, you can implement role-based authorization in your application. This package allows you to define policies based on roles. Also, you can use Keycloak as Authorization Server. It is a powerful way to organize and apply authorization polices centrally.

var builder = WebApplication.CreateBuilder(args);

var host = builder.Host;
var configuration = builder.Configuration;
var services = builder.Services;

services.AddKeycloakWebApiAuthentication(configuration);

services.AddAuthorization(options =>
    {
        options.AddPolicy("AdminAndUser", builder =>
        {
            builder
                .RequireRealmRoles("User") // Realm role is fetched from token
                .RequireResourceRoles("Admin"); // Resource/Client role is fetched from token
        });
    })
    .AddKeycloakAuthorization(configuration);

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapGet("/hello", () => "[]")
    .RequireAuthorization("AdminAndUser");

app.Run();

Build and Development

dotnet cake --target build

dotnet cake --target test

dotnet pack -o ./Artefacts

Blog Posts

For more information and real world examples, please see my blog posts related to Keycloak and .NET https://nikiforovall.github.io/tags.html#keycloak-ref

Reference

No packages depend on Keycloak.AuthServices.Common.

The release notes are available at https://github.com/NikiforovAll/keycloak-authorization-services-dotnet/releases

Version Downloads Last updated
3.0.0 3 06.05.2026
3.0.0-rc.1 2 29.04.2026
2.10.0-rc.1 2 27.04.2026
2.9.0 2 31.03.2026
2.8.1 2 23.03.2026
2.8.0 3 11.02.2026
2.7.0 6 25.09.2025
2.6.1 8 04.06.2025
2.6.0 8 04.06.2025
2.5.5 8 04.06.2025
2.5.3 9 29.04.2025
2.5.2 8 04.06.2025
2.5.1 9 04.06.2025
2.5.0 8 04.06.2025
2.4.1 8 04.06.2025
2.4.0 8 04.06.2025
2.3.0 8 04.06.2025
2.3.0-pre-1 8 04.06.2025
2.2.1 8 04.06.2025
2.2.0 8 04.06.2025
2.1.0 8 04.06.2025
2.0.0 8 04.06.2025
2.0.0-pre-4 8 04.06.2025
2.0.0-pre-3 8 04.06.2025
2.0.0-pre-2 8 04.06.2025
2.0.0-pre-1 8 04.06.2025
1.7.0 8 04.06.2025
1.6.0 8 04.06.2025
1.5.2 8 04.06.2025
1.5.1 7 04.06.2025
1.5.0 8 04.06.2025
1.4.1 8 26.05.2025
1.4.0 8 04.06.2025
1.3.0 8 04.06.2025
1.2.1 8 04.06.2025
1.2.0 8 04.06.2025
1.1.0 8 04.06.2025
1.0.5 8 04.06.2025
1.0.4 8 04.06.2025
1.0.3 8 04.06.2025
1.0.2 8 04.06.2025
1.0.1 8 04.06.2025