Xeku.Keycloak.WebApi
0.0.0.7
dotnet add package Xeku.Keycloak.WebApi --version 0.0.0.7
NuGet\Install-Package Xeku.Keycloak.WebApi -Version 0.0.0.7
<PackageReference Include="Xeku.Keycloak.WebApi" Version="0.0.0.7" />
<PackageVersion Include="Xeku.Keycloak.WebApi" Version="0.0.0.7" />
<PackageReference Include="Xeku.Keycloak.WebApi" />
paket add Xeku.Keycloak.WebApi --version 0.0.0.7
#r "nuget: Xeku.Keycloak.WebApi, 0.0.0.7"
#:package Xeku.Keycloak.WebApi@0.0.0.7
#addin nuget:?package=Xeku.Keycloak.WebApi&version=0.0.0.7
#tool nuget:?package=Xeku.Keycloak.WebApi&version=0.0.0.7
Xeku.Keycloak.WebApi
This module facilitates the integration of Keycloak JWT authentication into DevExpress XAF Web API applications (v22.2+). It supports hybrid authentication, allowing both XAF's native authentication and Keycloak's external authentication to coexist.
Features
- Hybrid Authentication: Seamlessly supports both XAF native JWT and Keycloak JWT tokens.
- Smart Routing: Automatically routes authentication requests to the correct scheme based on the Token Issuer using
ForwardDefaultSelector. - User Mapping: Provides a generic
KeycloakAuthenticationProviderto map Keycloak users (viapreferred_username) to XAF application users. - Easy Configuration: Simple
appsettings.jsonconfiguration.
Installation
- NuGet Package:
dotnet add package Xeku.Keycloak.WebApi
- In your
Startup.csorProgram.cs:
using Xeku.Keycloak.WebApi;
// ...
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddXafAspNetCoreSecurity(Configuration, options =>
{
// ...
})
.AddAuthenticationStandard(options =>
{
options.IsSupportChangePassword = true;
})
// 1. Register Keycloak Authentication Provider (Replace ApplicationUser with your XAF user class)
.AddAuthenticationProvider<KeycloakAuthenticationProvider<ApplicationUser>>();
// ...
// 2. Register Keycloak Web API Services
services.AddXekuKeycloakWebApi<ApplicationUser>(Configuration);
// ...
}
Configuration
Add the following section to your appsettings.json:
{
"Authentication": {
"Keycloak": {
"Authority": "http://localhost:8080/realms/your-realm",
"Audience": "your-client-id",
"RequireHttpsMetadata": false, // Set to true in production
"NameClaimType": "preferred_username" // Optional, default is "preferred_username"
}
}
}
Important Keycloak Configuration
This module enables Audience Validation (ValidateAudience = true) by default for security. You MUST configure a mapper in Keycloak, otherwise you will encounter 401 Unauthorized errors.
Follow these steps to configure the Audience Mapper:
- Log in to the Keycloak Admin Console.
- Go to Clients → Select your Client (e.g.,
xekuweb). - Go to the Client scopes tab.
- Click on
xekuweb-dedicated(usually named<client-id>-dedicated). - Click Add mapper → By configuration.
- Select Audience.
- In the configuration page:
- Name: Enter
Audience Mapper(or any name). - Included Client Audience: Enter your Client ID (e.g.,
xekuweb). - Add to access token: Toggle On.
- Name: Enter
- Click Save.
After this configuration, the Access Token issued by Keycloak will include your Client ID in the aud claim, allowing validation to pass.
How It Works
- Token Validation: The module adds a JWT Bearer handler named "Keycloak".
- Request Routing: It intercepts the
Authorizationheader. If the token's Issuer matches the configured Keycloak Authority, the request is forwarded to the "Keycloak" handler. Otherwise, it falls back to the default handler (XAF). - User Resolution: Once validated, the
KeycloakAuthenticationProviderextracts the user name (frompreferred_username) and searches for a matchingApplicationUserin the XAF database. If found, the user is logged in.
Cache Integration (Optional)
Inject ICacheService into KeycloakAuthenticationProvider for improved user lookup performance:
// Register with caching
services.AddSingleton<IAuthenticationProvider>(sp =>
new KeycloakAuthenticationProvider<ApplicationUser>(
sp.GetRequiredService<IHttpContextAccessor>(),
options,
sp.GetRequiredService<ILogger<KeycloakAuthenticationProvider<ApplicationUser>>>(),
sp.GetRequiredService<ICacheService>() // Optional cache service
));
| Cache Key | TTL | Description |
|---|---|---|
keycloak:user:{userName} |
5 min | User lookup result |
Requirements
- .NET 6.0 or higher (Targeting .NET 8.0)
- DevExpress XAF 22.2 or higher
- Xeku.Cache (optional, for caching support)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- DevExpress.ExpressApp.Security.Xpo (>= 25.2.3)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.15)
- Xeku.Cache (>= 0.0.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.