Skip to content

Tenancy

The Tenancy module provides per-request tenant extraction via ASP.NET Core middleware. It resolves tenant identifiers from JWT claims or HTTP headers and propagates them through CorrelationContext.TenantId.

Quick Start

using Deepstaging.Tenancy;

// In Program.cs
builder.Services.Configure<TenancyOptions>(
    builder.Configuration.GetSection("Deepstaging:Tenancy"));
app.UseTenantMiddleware();

// Configuration (appsettings.json)
{
    "Deepstaging": {
        "Tenancy": {
            "ClaimName": "tenant_id",
            "HeaderName": "ds-tenant-id",
            "RequireTenant": true,
            "ExcludedPaths": ["/health", "/auth"]
        }
    }
}

Features

Feature Description
TenantMiddleware ASP.NET Core middleware for per-request tenant resolution
TenancyOptions Configuration — claim name, header name, require flag, excluded paths
TenantId TypedId for tenant identifiers
CorrelationContext Propagates tenant via CorrelationContext.TenantId
Path exclusion Skip tenant resolution for health checks, auth endpoints, etc.

Resolution Order

The middleware resolves the tenant identifier in order:

  1. JWT claim — looks for the claim named by TenancyOptions.ClaimName (default: tenant_id)
  2. HTTP header — falls back to the header named by TenancyOptions.HeaderName (default: ds-tenant-id)

If neither source provides a tenant:

  • RequireTenant = true → 400 Bad Request
  • RequireTenant = false → request proceeds without tenant context

Configuration

Property Default Description
ClaimName "tenant_id" JWT claim name for tenant extraction
HeaderName "ds-tenant-id" HTTP header name (fallback)
RequireTenant true Reject requests without a tenant
ExcludedPaths [] Path prefixes that bypass tenant resolution

Integration with CorrelationContext

Once resolved, the tenant ID is set on the ambient CorrelationContext:

// Read the current tenant anywhere downstream
var tenantId = CorrelationContext.Current?.TenantId;

The tenant ID flows automatically through:

  • Dispatch command/query handlers
  • Event queue handlers
  • Background jobs
  • HTTP client correlation headers

Sub-Pages

Page Description
Effects Composition How tenant context flows through Eff<RT, T> pipelines
Generated Code TenantId TypedId and DI registration

Source

src/Core/Deepstaging.Runtime/Tenancy/