Athenex.Whimclean.Api.AspNetCore.Multitenancy 2.0.0

Prefix Reserved
dotnet add package Athenex.Whimclean.Api.AspNetCore.Multitenancy --version 2.0.0
                    
NuGet\Install-Package Athenex.Whimclean.Api.AspNetCore.Multitenancy -Version 2.0.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="Athenex.Whimclean.Api.AspNetCore.Multitenancy" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Athenex.Whimclean.Api.AspNetCore.Multitenancy" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Athenex.Whimclean.Api.AspNetCore.Multitenancy" />
                    
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 Athenex.Whimclean.Api.AspNetCore.Multitenancy --version 2.0.0
                    
#r "nuget: Athenex.Whimclean.Api.AspNetCore.Multitenancy, 2.0.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 Athenex.Whimclean.Api.AspNetCore.Multitenancy@2.0.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=Athenex.Whimclean.Api.AspNetCore.Multitenancy&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Athenex.Whimclean.Api.AspNetCore.Multitenancy&version=2.0.0
                    
Install as a Cake Tool

Athenex.Whimclean.Api.AspNetCore.Multitenancy

ภาษาไทย

Optional ASP.NET Core tenant resolution and membership authorization for Whimclean APIs.

Install

dotnet add package Athenex.Whimclean.Api.AspNetCore.Multitenancy

The package depends on Athenex.Whimclean.Api.AspNetCore for consistent tenant error responses and on the dependency-free multitenancy contracts in Athenex.Whimclean.Multitenancy.Abstractions. It contains no FastEndpoints code and works with FastEndpoints or Minimal APIs alike.

Implement membership validation

The consuming application owns tenants and memberships. Implement its authorization rules through ITenantAccessValidator<TUserId, TTenantId>:

using Athenex.Whimclean.Multitenancy;
using Microsoft.EntityFrameworkCore;

public sealed class TenantMembershipAccessValidator(
    AuthenticationDbContext dbContext)
    : ITenantAccessValidator<Guid, Guid>
{
    public Task<bool> HasAccessAsync(
        Guid userId,
        Guid tenantId,
        CancellationToken cancellationToken = default) =>
        dbContext.TenantMemberships.AnyAsync(
            membership =>
                membership.UserId == userId &&
                membership.TenantId == tenantId &&
                membership.IsActive,
            cancellationToken);
}

Include tenant state, membership expiry, suspension, or entitlement checks here when required. Implementations should fail closed: return false for unknown users or tenants.

Register the API integration

using Athenex.Whimclean.Api.AspNetCore;
using Athenex.Whimclean.Api.AspNetCore.Multitenancy;
using Athenex.Whimclean.Multitenancy;

builder.Services.AddMinimalApi();
builder.Services.AddAspNetCoreMultitenancy<Guid, Guid>();
builder.Services.AddHeaderTenantResolution<Guid>(options =>
    options.HeaderName = "X-Tenant-Id");
builder.Services.AddScoped<
    ITenantAccessValidator<Guid, Guid>,
    TenantMembershipAccessValidator>();

The first generic argument is the current-user ID type. The second is the tenant ID type.

AddAspNetCoreMultitenancy registers:

  • the scoped TenantContext<TTenantId>, exposed as both IRequestedTenant<TTenantId> and ICurrentTenant<TTenantId>
  • the TenantMember authorization policy (the name can be changed through the policyName parameter; MultitenancyPolicies.TenantMember holds the default)
  • the membership authorization handler
  • tenant exception mappings for the standard API error shape

AddHeaderTenantResolution<TTenantId> registers HeaderRequestedTenantResolver<TTenantId> as the IRequestedTenantResolver<TTenantId> (via TryAddScoped, so an existing resolver wins). The header name defaults to X-Tenant-Id (HeaderTenantResolutionOptions.HeaderName), and the value is parsed with the invariant culture. Exactly one header value is required: a missing header resolves nothing, and a repeated header is treated as ambiguous and resolves nothing.

Middleware order

Authentication must establish the user before tenant membership authorization runs:

app.UseAuthentication();
app.UseTenantResolution<Guid>();
app.UseAuthorization();
app.UseMinimalApi();

A FastEndpoints application registers AddFastEndpointsApi and calls UseFastEndpointsApi in the same positions.

The resolver only records a requested tenant. ICurrentTenant<Guid> remains empty until the authenticated user passes the access validator.

Protect tenant endpoints

using Athenex.Whimclean.Api.AspNetCore;
using Athenex.Whimclean.Api.AspNetCore.Multitenancy;

var tenantApi = app.MapApiGroup()
    .RequireAuthorization(MultitenancyPolicies.TenantMember);

tenantApi.MapGet("orders", () => Results.Ok());

A FastEndpoints endpoint applies the same policy with Policies(MultitenancyPolicies.TenantMember) in its Configure method.

Tenant-aware EF contexts resolved during the endpoint receive the authorized ICurrentTenant<TTenantId> from the same request scope.

Use a route, host, or claim resolver

Replace the header resolver by implementing IRequestedTenantResolver<TTenantId>:

using Athenex.Whimclean.Api.AspNetCore.Multitenancy;
using Microsoft.AspNetCore.Http;

public sealed class RouteTenantResolver
    : IRequestedTenantResolver<Guid>
{
    public bool TryResolve(HttpContext context, out Guid tenantId) =>
        Guid.TryParse(
            context.Request.RouteValues["tenantId"]?.ToString(),
            out tenantId);
}

services.AddScoped<IRequestedTenantResolver<Guid>, RouteTenantResolver>();

Do not call AddHeaderTenantResolution when a custom resolver is the only desired source.

Error responses

  • CurrentTenantRequiredException → 403 Tenancy.CurrentTenantRequired
  • TenantAccessViolationException → 403 Tenancy.AccessDenied
  • a missing or rejected requested tenant fails the TenantMember policy and produces the standard 403 body (Authorization.Forbidden)

The package does not create tenant tables, manage memberships, or decide whether Identity users are global or tenant-local. The recommended default is global authentication users plus consumer-owned tenant memberships.

Resolve the tenant from a slug

Deployments that serve each tenant from a path (/acme/...) or hostname forward the slug in a header; the API maps it to the tenant id:

using Athenex.Whimclean.Api.AspNetCore.Multitenancy;
using Microsoft.EntityFrameworkCore;

public sealed class TenantSlugLookup(ApplicationDbContext dbContext) : ITenantSlugLookup<Guid>
{
    public async ValueTask<Guid> FindBySlugAsync(string slug, CancellationToken ct = default) =>
        await dbContext.Tenants
            .Where(tenant => tenant.Slug == slug)
            .Select(tenant => tenant.Id)
            .SingleOrDefaultAsync(ct);
}

builder.Services.AddAspNetCoreMultitenancy<Guid, Guid>();
builder.Services.AddHeaderTenantResolution<Guid>();          // optional: X-Tenant-Id still works
builder.Services.AddSlugTenantResolution();                  // X-Tenant-Slug
builder.Services.AddScoped<ITenantSlugLookup<Guid>, TenantSlugLookup>();

app.UseAuthentication();
app.UseTenantResolution<Guid>();
app.UseSlugTenantResolution<Guid>();
app.UseAuthorization();

AddSlugTenantResolution is not generic: it only registers SlugTenantResolutionOptions (header name, default X-Tenant-Slug); the tenant id type is given to UseSlugTenantResolution<TTenantId> and ITenantSlugLookup<TTenantId>. The slug arrives trimmed and lower-cased. The slug middleware records nothing when a tenant was already resolved, so an explicit id header wins, and a default (empty) identifier means no such tenant. As with the id header, exactly one slug header value is required; a repeated header resolves nothing. Like the header resolver it only records a claim; membership is still validated by the TenantMember policy.

Repository and full architecture guide: https://github.com/athene-x/Whimclean

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
2.0.0 86 11/17/2026

2.0.0: first nuget.org release. Requested-tenant resolution from an id or slug header (through ITenantSlugLookup, id header taking precedence), membership authorization, and tenant API error mapping.