Keycloak.AuthServices.Authentication 2.10.0-rc.1
Keycloak.AuthServices
🔐 Easy Authentication and Authorization with Keycloak in .NET.
Documentation
For Developer Documentation see: https://nikiforovall.github.io/keycloak-authorization-services-dotnet
API Reference
See: https://nikiforovall.github.io/keycloak-authorization-services-dotnet-docs
Getting Started
Install packages:
dotnet add package Keycloak.AuthServices.Authentication
// 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 - Add 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();
Example - Invoke Admin API
var services = new ServiceCollection();
services.AddKeycloakAdminHttpClient(new KeycloakAdminClientOptions
{
AuthServerUrl = "http://localhost:8080/",
Realm = "master",
Resource = "admin-api",
});
var sp = services.BuildServiceProvider();
var client = sp.GetRequiredService<IKeycloakRealmClient>();
var realm = await client.GetRealmAsync("Test");
Build and Development
dotnet cake --target build
dotnet cake --target test
dotnet pack -o ./Artefacts
No packages depend on Keycloak.AuthServices.Authentication.
The release notes are available at https://github.com/NikiforovAll/keycloak-authorization-services-dotnet/releases
.NET 10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.5)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 10.0.5)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Configuration.Json (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.IdentityModel.Protocols.OpenIdConnect (>= 8.16.0)
| Version | Downloads | Last updated |
|---|---|---|
| 3.0.0 | 3 | 04.05.2026 |
| 3.0.0-rc.1 | 2 | 28.04.2026 |
| 2.10.0-rc.1 | 2 | 29.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 | 4 | 19.12.2025 |
| 2.6.1 | 6 | 24.06.2025 |
| 2.6.0 | 5 | 24.06.2025 |
| 2.5.3 | 6 | 29.04.2025 |
| 2.5.2 | 5 | 24.06.2025 |
| 2.5.1 | 5 | 24.06.2025 |
| 2.5.0 | 6 | 29.04.2025 |
| 2.4.1 | 5 | 24.06.2025 |
| 2.4.0 | 5 | 24.06.2025 |
| 2.3.0 | 5 | 24.06.2025 |
| 2.3.0-pre-1 | 5 | 25.06.2025 |
| 2.2.1 | 5 | 24.06.2025 |
| 2.2.0 | 5 | 24.06.2025 |
| 2.1.0 | 5 | 24.06.2025 |
| 2.0.0 | 6 | 24.06.2025 |
| 2.0.0-pre-4 | 5 | 25.06.2025 |
| 2.0.0-pre-3 | 5 | 25.06.2025 |
| 2.0.0-pre-2 | 6 | 25.06.2025 |
| 2.0.0-pre-1 | 6 | 25.06.2025 |
| 1.6.0 | 5 | 24.06.2025 |
| 1.5.2 | 6 | 24.06.2025 |
| 1.5.1 | 5 | 24.06.2025 |
| 1.5.0 | 5 | 24.06.2025 |
| 1.4.1 | 6 | 24.06.2025 |
| 1.4.0 | 6 | 24.06.2025 |
| 1.3.0 | 6 | 24.06.2025 |
| 1.2.1 | 5 | 24.06.2025 |
| 1.2.0 | 5 | 24.06.2025 |
| 1.1.0 | 5 | 24.06.2025 |
| 1.0.5 | 5 | 24.06.2025 |
| 1.0.4 | 6 | 24.06.2025 |
| 1.0.3 | 5 | 24.06.2025 |
| 1.0.2 | 5 | 24.06.2025 |
| 1.0.1 | 5 | 24.06.2025 |
| 1.0.0 | 5 | 24.06.2025 |