Keycloak.AuthServices.Common 2.0.0
Keycloak.AuthServices
Easy Authentication and Authorization with Keycloak in .NET and ASP.NET Core.
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
- https://nikiforovall.github.io/aspnetcore/dotnet/2022/08/24/dotnet-keycloak-auth.html
- https://nikiforovall.github.io/dotnet/keycloak/2022/12/28/keycloak-authorization-server.html
- https://nikiforovall.github.io/blazor/dotnet/2022/12/08/dotnet-keycloak-blazorwasm-auth.html
Reference
No packages depend on Keycloak.AuthServices.Common.
.NET 6.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.0)
| Version | Downloads | Last updated |
|---|---|---|
| 3.0.0 | 2 | 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 |