Keycloak

Dosaic.Plugins.Authorization.Keycloak is a plugin that provides JWT-based authentication and role-driven authorization against a Keycloakarrow-up-right 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 keycloak authentication scheme — validates JWT Bearer tokens without a full OIDC discovery round-trip; fetches the realm public key once and caches it in memory

  • Realm & resource-access role mapping — automatically extracts roles from the Keycloak realm_access and resource_access JWT claims and adds them as ClaimTypes.Role claims

  • Named authorization policies — define policies by name with required roles in configuration; policies are registered with ASP.NET Core's IAuthorizationService automatically

  • Disable mode — set Enabled: false to run without any authentication (an allow-all default policy is registered), useful for local development

  • Health check integration — registers a URL health check against the Keycloak management endpoint (defaults to port 9000, path /health/ready) tagged as readiness

  • OpenTelemetry instrumentation — emits authentication_keycloak_success and authentication_keycloak_noresult counters plus distributed tracing spans per authentication attempt

  • Separate 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.Keycloak

Or 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

Disable 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

  1. The Authorization: Bearer <token> header is extracted from the incoming request.

  2. The JWT is decoded to read the iss (issuer) claim, which contains the Keycloak realm URL.

  3. PublicKeyService fetches the realm's public key from <authority><realms.prefix>/<realm> and caches it in memory for the lifetime of the process.

  4. The token is validated using JwtSecurityTokenHandler with ValidateIssuerSigningKey = true, ValidateLifetime = true, and ValidateAudience = false.

  5. Roles are extracted from the realm_access.roles array and — when a client ID is present (azp claim) — also from resource_access.<clientId>.roles.

  6. All extracted roles are added as ClaimTypes.Role claims, making them available to [Authorize(Roles = "...")] and RequireRole policies.

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