Keycloak
Dosaic.Plugins.Authorization.Keycloak is a plugin that provides JWT-based authentication and role-driven authorization against a Keycloak server. It implements a custom ASP.NET Core authentication scheme that validates Bearer tokens using Keycloak's realm public key, maps realm and client roles to ClaimTypes.Role, and exposes named authorization policies that can be applied to minimal API endpoints or MVC controllers.
Features
Custom
keycloakauthentication scheme — validates JWT Bearer tokens without a full OIDC discovery round-trip; fetches the realm public key once and caches it in memoryRealm & resource-access role mapping — automatically extracts roles from the Keycloak
realm_accessandresource_accessJWT claims and adds them asClaimTypes.RoleclaimsNamed authorization policies — define policies by name with required roles in configuration; policies are registered with ASP.NET Core's
IAuthorizationServiceautomaticallyDisable mode — set
Enabled: falseto run without any authentication (an allow-all default policy is registered), useful for local developmentHealth check integration — registers a URL health check against the Keycloak management endpoint (defaults to port
9000, path/health/ready) tagged asreadinessOpenTelemetry instrumentation — emits
authentication_keycloak_successandauthentication_keycloak_noresultcounters plus distributed tracing spans per authentication attemptSeparate health-check host — the health check target can differ from the authentication authority (e.g. internal management port vs. public HTTPS endpoint)
Installation
dotnet add package Dosaic.Plugins.Authorization.KeycloakOr add the package reference directly to your .csproj:
<PackageReference Include="Dosaic.Plugins.Authorization.Keycloak" Version="" />Configuration
The plugin is configured under the keycloak key via the [Configuration("keycloak")] attribute on KeycloakPluginConfiguration.
Configuration classes
appsettings.yml example
appsettings.yml exampleDisable for local development
When enabled is false, no authentication is required and all requests are allowed through. A warning is logged at startup.
Usage
Minimal API endpoints
MVC controllers
How token validation works
The
Authorization: Bearer <token>header is extracted from the incoming request.The JWT is decoded to read the
iss(issuer) claim, which contains the Keycloak realm URL.PublicKeyServicefetches the realm's public key from<authority><realms.prefix>/<realm>and caches it in memory for the lifetime of the process.The token is validated using
JwtSecurityTokenHandlerwithValidateIssuerSigningKey = true,ValidateLifetime = true, andValidateAudience = false.Roles are extracted from the
realm_access.rolesarray and — when a client ID is present (azpclaim) — also fromresource_access.<clientId>.roles.All extracted roles are added as
ClaimTypes.Roleclaims, making them available to[Authorize(Roles = "...")]andRequireRolepolicies.
Authorization policies
Policies are registered from KeycloakPluginConfiguration.Policies and require the user to have all listed roles:
The default policy requires an authenticated user (i.e., a valid Keycloak token). Unauthenticated requests to unprotected endpoints are still allowed.
Health check
The plugin registers a URL health check named keycloak tagged as readiness:
With the defaults above this resolves to http://keycloak-internal.example.com:9000/health/ready. The health check returns Unhealthy if the endpoint does not respond with a success status code.
Last updated