MVFC.Aspire.Helpers.Keycloak 13.1.0

dotnet add package MVFC.Aspire.Helpers.Keycloak --version 13.1.0
                    
NuGet\Install-Package MVFC.Aspire.Helpers.Keycloak -Version 13.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MVFC.Aspire.Helpers.Keycloak" Version="13.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Aspire.Helpers.Keycloak" Version="13.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Aspire.Helpers.Keycloak" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MVFC.Aspire.Helpers.Keycloak --version 13.1.0
                    
#r "nuget: MVFC.Aspire.Helpers.Keycloak, 13.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MVFC.Aspire.Helpers.Keycloak@13.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MVFC.Aspire.Helpers.Keycloak&version=13.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Aspire.Helpers.Keycloak&version=13.1.0
                    
Install as a Cake Tool

MVFC.Aspire.Helpers.Keycloak

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

This package provides extension methods for .NET Aspire, simplifying the integration, configuration, and initialization of a Keycloak server to manage local environment identity and access.

With it, you can not only initialize an official Keycloak container but also inject configurations (like BaseUrl, Realm, ClientId, etc.) and import custom realms (via JSON seeds) at development time.

Motivation

Calling WithSeeds more than once replaces the configured seed list. Calling it with an empty list clears prior seeds.

Keycloak is powerful, but local setup is often painful:

  • Large docker commands with many environment variables and import flags.
  • Manually importing realms, clients and users from JSON files.
  • Repeating the same setup for every project or demo.

With .NET Aspire you can define a Keycloak container, but you still need to:

  • Track admin credentials, ports and volumes.
  • Decide how to seed realms/clients/users in a repeatable way.
  • Make your applications wait for Keycloak to be ready.

MVFC.Aspire.Helpers.Keycloak turns this into a small, opinionated API:

  • AddKeycloak(...) runs Keycloak with sensible defaults for dev.
  • WithAdminCredentials(...), WithDataVolume(...) and WithSeeds(...) centralize Keycloak‑specific concerns.
  • project.WithReference(keycloak, ...) wires your projects to Keycloak with the correct dependencies and configuration.

Features and Capabilities

  • Embedded Server: Starts an official Keycloak container (currently quay.io/keycloak/keycloak:26.1).
  • Default Authentication: Optional configuration with a pre-defined ClientId and seeds for developers.
  • Customizable Port: Fixed or dynamic port mapping by Aspire.
  • Transparent Injection: Adds authentication endpoints to .WithReference() projects, configuring URLs, realms, and ClientIds as environment variables.
  • Persistent Volume: Optionally saves state and internal databases.
  • Dynamic Realm Import (Seeds): Bootstrap your development environment by populating realms, clients, roles, and users via code configuration.

Installation

Add the package to the main AppHost project in your .NET Aspire solution:

dotnet add package MVFC.Aspire.Helpers.Keycloak
dotnet add package Aspire.Hosting.Dotnet --version 17.0.0

Commands and examples using AddDotnetProject target a .NET 10/11 AppHost on Aspire 17. For .NET 9 AppHosts, keep Aspire 13.6.5, omit Aspire.Hosting.Dotnet 17 and use AddProject<Projects.MyApi>("api"). Adjust the project path to your AppHost.

Quick Aspire usage (AppHost)

using Aspire.Hosting;
using MVFC.Aspire.Helpers.Keycloak;
using MVFC.Aspire.Helpers.Keycloak.Configs;
using MVFC.Aspire.Helpers.Keycloak.Seeds;

var builder = DistributedApplication.CreateBuilder(args);

// Adding a basic Keycloak container
var keycloak = builder.AddKeycloak("keycloak", httpPort: 8081)
    .WithAdminCredentials("admin", "admin")
    .WithSeeds(new MyAppRealm());

var api = builder.AddDotnetProject("api", "../My_Api/My_Api.csproj")
    .WithReference(keycloak, realmName: "my-app-realm", clientId: "my-api", clientSecret: "api-secret-1234")
    .WaitFor(keycloak);

await builder.Build().RunAsync();

internal sealed class MyAppRealm : IKeycloakRealmSeed
{
    public string RealmName => "my-app-realm";

    public IReadOnlyList<KeycloakClientConfig> Clients =>
    [
        new(ClientId: "my-api", ClientSecret: "api-secret-1234",
            DirectAccessGrantsEnabled: true, AddAudienceMapper: true),
    ];
}

Using the injected configuration in the API

By default, when using .WithReference(keycloak, realmName, clientId, clientSecret), Aspire injects the following configuration keys into your IConfiguration:

Configuration Key Description Example
Keycloak:BaseUrl Full URL to access the Keycloak container. http://localhost:8081
Keycloak:Realm Realm name propagated from the AppHost. my-app-realm
Keycloak:ClientId OAuth/OpenID client ID. my-api
Keycloak:ClientSecret (Optional) Secret associated with the client. api-secret-1234

Example in your API:

var baseUrl  = builder.Configuration["Keycloak:BaseUrl"];
var realm    = builder.Configuration["Keycloak:Realm"];
var clientId = builder.Configuration["Keycloak:ClientId"];

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.RequireHttpsMetadata = false; // dev
        options.Audience = clientId;
        options.MetadataAddress = $"{baseUrl}/realms/{realm}/.well-known/openid-configuration";
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidIssuer = $"{baseUrl}/realms/{realm}",
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidAudience = clientId
        };
    });

builder.Services.AddAuthorization();

Provisioning diagram

sequenceDiagram
    participant Aspire as .NET Aspire
    participant Container as Keycloak Container
    participant App as Consumer

    Aspire->>Aspire: BeforeStartEvent generates realm JSON files from IKeycloakRealmSeed
    Aspire->>Container: Start Keycloak with import files and --import-realm
    Container->>Container: Import configured realms, clients, users and roles
    Aspire->>Container: Probe management endpoint /health/ready
    Container-->>Aspire: HTTP health check passes
    Aspire->>App: Start consumer with Keycloak settings (WaitFor)

AddKeycloak() options

Parameter Type Description Default
name string Required resource name in Aspire. —
httpPort int HTTP port exposed to consumers. 8081
managementPort int Management port used by the /health/ready check. 9000

Additional extensions

  • .WithAdminCredentials(username, password) – sets administrator credentials (local default: admin/admin).
  • .WithSeeds(seeds) – accepts IKeycloakRealmSeed implementations, generates JSON files before startup and enables --import-realm.
  • .WithImportStrategy(strategy) – selects the realm import strategy.
  • .WithDataVolume(volumeName) – persists data in a named Docker volume.

Requirements

  • .NET 9, 10 or 11
  • Aspire.Hosting 13.6.5 on .NET 9; Aspire.Hosting 17.0.0 on .NET 10/11. AppHost examples use Aspire.Hosting.Dotnet 17 and AddDotnetProject; .NET 9 AppHosts retain the ProjectResource model.
  • Docker running locally

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 is compatible.  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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
13.1.0 47 11/29/2026
13.0.0 53 11/28/2026
12.3.0 51 11/27/2026
12.2.1 52 11/27/2026
12.2.0 88 11/26/2026
12.1.0 83 11/26/2026
12.0.0 87 11/25/2026
11.0.2 100 11/21/2026
11.0.1 102 11/20/2026
11.0.0 99 11/13/2026
10.1.7 106 11/8/2026
10.1.6 98 11/8/2026
10.1.4 98 11/7/2026
10.1.3 120 11/1/2026
10.1.2 108 11/1/2026
10.1.1 110 10/31/2026
10.1.0 156 8/31/2026
10.0.0 141 8/30/2026
9.0.5 128 8/28/2026
9.0.4 162 6/26/2026
Loading failed