Dosaic.Plugins.Authorization.Zitadel 1.2.53

dotnet add package Dosaic.Plugins.Authorization.Zitadel --version 1.2.53
                    
NuGet\Install-Package Dosaic.Plugins.Authorization.Zitadel -Version 1.2.53
                    
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="Dosaic.Plugins.Authorization.Zitadel" Version="1.2.53" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dosaic.Plugins.Authorization.Zitadel" Version="1.2.53" />
                    
Directory.Packages.props
<PackageReference Include="Dosaic.Plugins.Authorization.Zitadel" />
                    
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 Dosaic.Plugins.Authorization.Zitadel --version 1.2.53
                    
#r "nuget: Dosaic.Plugins.Authorization.Zitadel, 1.2.53"
                    
#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 Dosaic.Plugins.Authorization.Zitadel@1.2.53
                    
#: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=Dosaic.Plugins.Authorization.Zitadel&version=1.2.53
                    
Install as a Cake Addin
#tool nuget:?package=Dosaic.Plugins.Authorization.Zitadel&version=1.2.53
                    
Install as a Cake Tool

Dosaic.Plugins.Authorization.Zitadel

Dosaic.Plugins.Authorization.Zitadel is a plugin that provides OAuth2 token-introspection authentication against a Zitadel server. It registers the official Zitadel authentication scheme, validates Bearer tokens via the Zitadel introspection endpoint (using a JWT application profile), and exposes a management service for programmatically creating and managing service accounts and user accounts.

Features

  • OAuth2 token introspection — validates Bearer tokens through Zitadel's introspection endpoint using an Application JWT profile; no manual key fetching required
  • Built-in response caching — introspection results are cached in the distributed cache to reduce round-trips; cache duration and key prefix are configurable
  • Discovery policy control — configure whether HTTPS is required for the discovery endpoint, and whether issuer/endpoint validation is enforced
  • IManagementService — injectable service to list service accounts, create service accounts (with JWT keys), create human user accounts, and obtain Bearer tokens for service accounts
  • Authentication failure logging — all authentication failures are logged automatically via the plugin's ILogger
  • Token from a cookie on given paths — optional, for server-rendered pages such as the Hangfire dashboard that a browser opens without an Authorization header
  • Project roles as role claims — optional, so User.IsInRole("<role>") also works for the project-scoped roles claim

Installation

dotnet add package Dosaic.Plugins.Authorization.Zitadel

Or add the package reference directly to your .csproj:

<PackageReference Include="Dosaic.Plugins.Authorization.Zitadel" Version="" />

Configuration

The plugin is configured under the Zitadel key via the [Configuration("Zitadel")] attribute on ZitadelConfiguration.

Configuration class

[Configuration("Zitadel")]
public class ZitadelConfiguration
{
    // Zitadel project ID (used to build the audience scope for service account tokens)
    public required string ProjectId { get; set; }

    // Zitadel organization ID (used when creating users/service accounts)
    public required string OrganizationId { get; set; }

    // Base URL of the Zitadel instance, e.g. "https://my-org.zitadel.cloud"
    public required string Host { get; set; }

    // When true (default), the authority URL uses https://; set to false for local/dev environments
    public bool UseHttps { get; set; } = true;

    // Whether to validate the issuer during OIDC discovery (default: true)
    public bool ValidateIssuer { get; set; } = true;

    // Whether to validate discovery endpoints (default: true)
    public bool ValidateEndpoints { get; set; } = true;

    // Enable caching of introspection results (default: true)
    public bool EnableCaching { get; set; } = true;

    // How long introspection results are cached (default: 1 minute)
    public int CacheDurationInMinutes { get; set; } = 1;

    // Prefix for cache keys (default: "ZITADEL_")
    public string CacheKeyPrefix { get; set; } = "ZITADEL_";

    // JSON string of the Zitadel Application JWT profile used for introspection
    public required string JwtProfile { get; set; }

    // JSON string of the Zitadel Service Account JWT profile used by IManagementService
    public string ServiceAccount { get; set; }

    // Read the access token from a cookie on the configured paths only (default: null = header only)
    public ZitadelCookieTokenConfiguration CookieToken { get; set; }

    // Add a ClaimTypes.Role claim per project role (default: false)
    public bool MapProjectRoles { get; set; }
}

public class ZitadelCookieTokenConfiguration
{
    // Name of the cookie that holds the access token
    public string Name { get; set; }

    // Request paths (after PathBase) on which the cookie is read; required when Name is set
    public string[] Paths { get; set; } = [];
}

Note: Passing an http:// host while UseHttps = true will throw an InvalidConfigurationException at startup.

appsettings.yml example

Zitadel:
  projectId: "23456789012345678"
  organizationId: "123456789012345678"
  host: https://my-org.zitadel.cloud
  useHttps: true
  validateIssuer: true
  validateEndpoints: true
  enableCaching: true
  cacheDurationInMinutes: 1
  cacheKeyPrefix: "ZITADEL_"
  jwtProfile: |
    {
      "type": "application",
      "keyId": "...",
      "key": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----\n",
      "appId": "...",
      "clientId": "..."
    }
  serviceAccount: |
    {
      "type": "serviceaccount",
      "keyId": "...",
      "key": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----\n",
      "userId": "..."
    }

Disable HTTPS for local development

Zitadel:
  host: http://localhost:8080
  useHttps: false
  validateIssuer: false
  validateEndpoints: false
  projectId: "1"
  organizationId: "1"
  jwtProfile: "{}"
  serviceAccount: "{}"

When useHttps is false, the plugin uses http:// for the authority URL and relaxes discovery validation — useful when running Zitadel locally via Docker.

Usage

Minimal API endpoints

public class MyEndpoints : IPluginEndpointsConfiguration
{
    public void ConfigureEndpoints(IEndpointRouteBuilder endpointRouteBuilder, IServiceProvider serviceProvider)
    {
        endpointRouteBuilder.MapGet("/hello", () => "Hello World!")
            .RequireAuthorization();

        endpointRouteBuilder.MapGet("/admin", () => "Admin area")
            .RequireAuthorization(policy => policy.RequireRole("admin"));
    }
}

MVC controllers

[ApiController]
[Route("api/[controller]")]
public class ItemsController : ControllerBase
{
    [HttpGet]
    [Authorize]
    public IActionResult GetAll() => Ok();

    [HttpGet("public")]
    [AllowAnonymous]
    public IActionResult Public() => Ok("no auth needed");
}

How token validation works

  1. The Authorization: Bearer <token> header is extracted from the incoming request (or, with cookieToken, the cookie on the configured paths, see below).
  2. The token is sent to Zitadel's introspection endpoint (<host>/oauth/v2/introspect) together with the Application JWT profile loaded from JwtProfile.
  3. Zitadel returns the token metadata (active, subject, scopes, roles, etc.) which is mapped to the ClaimsPrincipal.
  4. If EnableCaching = true, the introspection result is stored in the distributed cache under <CacheKeyPrefix><token-hash> for CacheDurationInMinutes to avoid repeated network calls.
  5. On any authentication failure the error is logged at Error level by the plugin's ILogger<ZitadelPlugin>.

A server-rendered page, such as the Hangfire dashboard, is opened by a browser navigation, which carries no Authorization header. The application can store the user's access token in a cookie instead and let the "ZITADEL" scheme read it, but only on the paths that need it:

zitadel:
  cookieToken:
    name: hangfire-token
    paths: ["/hangfire"]
  • On a request whose Request.Path (after PathBase) lies below one of paths, and that has no Authorization header at all, the token is taken from the cookie. Paths match per segment and ignore case: /hangfire matches /hangfire and /hangfire/stats, not /hangfirex.
  • On every other path the cookie is ignored. This is a CSRF guard: a browser attaches the cookie on its own, so it must never authenticate the GraphQL or REST API. A GraphQL POST that carries only the cookie stays 401.
  • An Authorization header always wins, a cookie is never combined with it.
  • paths is required when name is set, and / is refused because it would cover every path. Both stop the start with an InvalidConfigurationException.
  • The cookie is only read; issuing it is the application's job. Use HttpOnly, Secure, SameSite=Strict and set its Path to the public path of the page (with the reverse proxy prefix, e.g. /p/<slug>/api/hangfire).

Behind a reverse proxy that strips a prefix and sends X-Forwarded-Prefix, the web host moves that prefix into Request.PathBase; paths are matched against the rest, so /hangfire stays correct for every prefix. See the Hangfire plugin's "Role-gated dashboard behind a reverse proxy" section for the full setup.

Project roles as role claims (zitadel:mapProjectRoles)

Zitadel returns the roles of a user as JSON objects whose keys are the role names, in urn:zitadel:iam:org:project:roles and in the project-scoped urn:zitadel:iam:org:project:<projectId>:roles:

{ "urn:zitadel:iam:org:project:123:roles": { "hangfire-admin": { "456": "org.example.com" } } }

The Zitadel library maps the first claim to ClaimTypes.Role claims, not the project-scoped one. With

zitadel:
  mapProjectRoles: true

the plugin adds one ClaimTypes.Role claim per key of both claims after the token was introspected, so User.IsInRole("hangfire-admin"), [Authorize(Roles = "hangfire-admin")] and RequireRole("hangfire-admin") work whichever shape the token carries. Only the project-scoped claim of the configured projectId is mapped, roles of other projects are ignored. A role the user already has is not added twice. The default false leaves the claims as they are.

Management service

IManagementService is registered automatically and can be injected to manage Zitadel users programmatically.

public interface IManagementService
{
    // List all machine (service account) users in the configured organization
    Task<List<ListServiceAccountResultItem>> ListServiceAccountsAsync();

    // Create a new service account user and return its JWT key content
    Task<ServiceAccountCreationResult> CreateServiceAccountAsync(
        string userid, string name, Timestamp keyExpirationTime, string description = "");

    // Obtain a Bearer token for a service account identified by its JSON key string
    Task<string> GetBearerTokenForServiceAccountAsync(string jsonString);

    // Create a human user account with an initial password
    Task<string> CreateUserAccountAsync(
        string userid, string email,
        string displayName, string givenName, string familyName, string password);
}

public record ServiceAccountCreationResult(string UserId, string KeyId, string KeyContent);

public record ListServiceAccountResultItem(
    string UserId, string State, string Name, string Description,
    bool HasSecret, string Owner, DateTime CreatedAt, DateTime UpdatedAt);

Example — list service accounts

public class MyService(IManagementService management)
{
    public async Task PrintServiceAccounts()
    {
        var accounts = await management.ListServiceAccountsAsync();
        foreach (var account in accounts)
            Console.WriteLine($"{account.UserId} — {account.Name} ({account.State})");
    }
}

Example — create a service account

var expirationTime = Timestamp.FromDateTime(DateTime.UtcNow.AddYears(1));
var result = await management.CreateServiceAccountAsync(
    userid: "my-service-id",
    name: "my-service",
    keyExpirationTime: expirationTime,
    description: "Service account for my-service");

Console.WriteLine($"UserId: {result.UserId}");
Console.WriteLine($"Key JSON: {result.KeyContent}");

Calling an API protected by Zitadel from a service account client

Use GetBearerTokenForServiceAccountAsync (or call the Zitadel SDK directly) to obtain a Bearer token for a service account and attach it to outgoing HTTP requests.

// Using IManagementService (reads service account JSON from configuration)
var token = await managementService.GetBearerTokenForServiceAccountAsync(jwtProfileJsonString);

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://<api-url>/api/endpoint");
request.Headers.Add("Authorization", "Bearer " + token);
var response = await client.SendAsync(request);

Or call the Zitadel SDK directly when you need additional scopes:

var authOptions = new ServiceAccount.AuthOptions();
// be sure to include this scope or your introspection will not be able to verify token for the indented project
authOptions.AdditionalScopes.Add("urn:zitadel:iam:org:project:id:123456789012345678:aud");

authOptions.AdditionalScopes.Add("offline_access");
authOptions.AdditionalScopes.Add("profile");
authOptions.AdditionalScopes.Add("email");

var token = await ServiceAccount.LoadFromJsonString("<jwtProfileJsonString>")
    .AuthenticateAsync("<HostUrl>", authOptions);

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://<api-url>/api/endpoint");
request.Headers.Add("Authorization", "Bearer " + token);
var response = await client.SendAsync(request);

The urn:zitadel:iam:org:project:id:<projectId>:aud scope tells Zitadel to include your project's audience in the token so the receiving API's introspection call succeeds.

Product Compatible and additional computed target framework versions.
.NET 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. 
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
1.2.53 107 11/16/2026
1.2.52 99 11/16/2026
1.2.51 97 11/13/2026
1.2.49 112 11/11/2026
1.2.48 103 11/4/2026
1.2.47 104 11/3/2026
1.2.45 113 10/30/2026
1.2.44 147 10/14/2026
1.2.43 127 10/13/2026
1.2.42 118 9/24/2026
1.2.41 148 9/7/2026
1.2.40 148 8/27/2026
1.2.40-alpha.1 94 8/27/2026
1.2.39 141 8/25/2026
1.2.38 157 7/30/2026
1.2.37 165 7/14/2026
1.2.36 155 7/10/2026
1.2.35 190 6/19/2026
1.2.34 154 6/10/2026
1.2.33 163 6/2/2026
Loading failed