ADR-0022: Tenant on the principal
- Status: accepted
- Date: 2026-10-07
- Supersedes: the tenant-resolution and per-aggregate wiring bullets of ADR-0019 (the toggle, the interceptor and the named-filter pattern stand)
- Amended: 2026-11-12 — a unique index on an aggregate root is unique per tenant, by the same convention (see Amendment (2026-11-12) below)
Context
Section titled “Context”ADR-0019 shipped the multi-tenancy toggle with a resolver that read the principal's tenant claim
first and fell back to an X-Tenant-Id request header. Nothing ever minted the claim:
ApplicationUser had no tenant, AuthenticatedUser carried none and JwtTokenProvider emitted
sub, email, jti and permission only. The claim branch was dead code and the header was the
selector, so any authenticated caller chose its tenant per request, and the "Tenant" query
filter, the TenantInterceptor stamp and the tenant-prefixed cache keys all faithfully scoped to
that choice. The only tests drove a hand-rolled ITenantContext, so the HTTP resolver was never
exercised.
Two smaller problems rode along. The shadow column, its index and the filter were hand-wired per
aggregate (AppDbContext.OnModelCreating for the filter, InvoiceConfiguration for the column),
so the second aggregate a consumer added shipped unisolated unless they remembered both places, and
no fitness test would have noticed.
Constraints: Result over exceptions; Infrastructure and Persistence stay peers (the tenant flows
Persistence → AuthenticatedUser (Application) → Infrastructure's token provider, exactly as the
permissions already do); no new package or project; the toggle-off output is unchanged in
behaviour; the shipped InitialCreate migration is generated for the toggle-off shape and stays so.
Decision
Section titled “Decision”- Tenant membership lives on the user.
ApplicationUser.TenantIdis a required, indexed string (max 64; opaque — the template never parses it). One user, one tenant. A membership table with a tenant switcher is an additive retrofit later; the claim, resolver and filter would not change. Identity tables stay global (no tenant filter): login, lockout and refresh run before a tenant is known, andRequireUniqueEmailalready means one e-mail → one user → one tenant. - The tenant reaches the token through the existing principal record.
AuthenticatedUsergainsTenantId;IdentityServicefills it from the user row;JwtTokenProvidermints it as the claim named byITenantContext.ClaimType("tenant"), the single constant for the name. - Resolution is claim-only; the header is gone.
TenantContextresolvesAmbientTenant(outbox dispatch restoring the originating tenant) → thetenantclaim →null. No anonymous endpoint touches tenant-scoped data, so a header branch has no legitimate consumer and is a trap for the next public endpoint someone adds. - Fail closed.
JwtBearerEvents.OnTokenValidatedrejects a token that validates but carries no tenant claim (a token minted before the toggle was enabled, or an external IdP that does not emit it); the existing challenge writes the RFC 9457 401. Letting such a principal fall through tonullwould be a silent read of system-scoped rows — the class of bug being fixed. - Scoping is a convention over every aggregate root.
AppDbContext.OnModelCreatingloops over the entity types whose CLR type implementsIHasDomainEvents(whatAggregateRoot<>implements and whatOutboxWriteInterceptoralready keys on) and gives each theTenantIdshadow column, its index and the named"Tenant"filter, built as an expression whose constant is the context instance so EF parameterises the current tenant per query.InvoiceConfigurationcontains no tenant code. An opt-in marker was rejected: forgetting it recreates "second aggregate ships unisolated". A deliberately shared aggregate is a future, explicit opt-out made in one place. - Self-registration provisions a new tenant (
Guid.CreateVersion7()), insideIdentityService.RegisterAsync, behind theRegistration:Modecheck inRegisterCommandHandler(closed outside Development by default). A caller-supplied tenant at registration would be the header hole again; invitation flows are out of template scope. - The seeded admin and the demo data belong to
Seed:TenantId(defaultdefault). The migration worker seeds the demo invoices underAmbientTenant= that tenant, so the admin sees them and the idempotent "already seeded" probe sees them too. Roles and permissions stay global. - The migration story is unchanged. The repository ships
InitialCreatefor the toggle-off model, which this decision does not touch; a project generated with--use-multitenancyregenerates it (migrations remove --force+add InitialCreate), as the template repository's all-options CI leg anddocs/development.mdalready do. The regenerated migration additionally carriesAspNetUsers.TenantId+ index next to the aggregate and outbox columns.
Proof lives in TenantIsolationTests (ApiTests, all-options leg: cross-tenant read and write 404,
overdue listing scoped, X-Tenant-Id ignored on reads and writes, a cache warmed by one tenant
never serving another, the token carrying the claim, a token without it rejected 401 while its
twin with the claim is accepted) and TenancyTests (IntegrationTests: model fitness for every
aggregate root and for the tables that must stay unfiltered, registration provisioning distinct
tenants, outbox rows carrying the tenant and handlers running under it).
Consequences
Section titled “Consequences”- A new aggregate is tenant-isolated the moment it derives from
AggregateRoot<>; the fitness test fails if that convention ever regresses. Never add tenant columns or filters in a configuration (.claude/rules/persistence.md). - Moving a user between tenants takes effect at the next token refresh (≤
AccessTokenMinutes). IHttpContextAccessoris an AsyncLocal and HybridCache runsGetOrCreateAsyncfactories without the caller's ExecutionContext, so a handler asking for the tenant inside a cache factory sees no request.TenantContexttherefore reads the principal live whenever the request is visible and remembers it for the scope; a detached flow gets what the request flow last resolved (the cache key, computed before the factory). Any other request-scoped service that reads the accessor inside a cache factory (CurrentUser) has the same blind spot; keep writes, and anything keyed on the principal, out of cache factories.- Replacing
JwtTokenProviderwith an external identity provider requires that provider to emit thetenantclaim, or every call is 401 — by design. - A cross-tenant administrative view must opt out per query with
IgnoreQueryFilters(["Tenant"]); the bareIgnoreQueryFilters()drops the tenant filter together with"SoftDeleted"silently (a source-scan guard is a separate decision). - A deliberately shared aggregate needs an explicit opt-out that does not exist yet; adding one is a deliberate change to the convention loop, not a per-aggregate flag.
- Enabling the toggle on a project that already has tenant-less users requires a data migration
that assigns each user a
TenantIdbefore the regenerated schema'sNOT NULLapplies; the template's regenerate-InitialCreatestory assumes a fresh database. - The outbox processor restores the originating tenant around each message's handlers (as before); a handler that writes under the tenant still depends on the per-message commit fix tracked with the outbox work.
Amendment (2026-11-12): a unique key is unique per tenant
Section titled “Amendment (2026-11-12): a unique key is unique per tenant”Decision 5 gave every aggregate root the TenantId column, its index and the filter, and left the
root's own unique indexes global. A feature's unique business key (a customer's e-mail, a code) then
disagreed with its own lookup: the tenant-filtered "is it taken?" query answered free, the global
index answered taken, and the create failed on a row the caller could not see, its 409 telling the
caller that another tenant holds the value.
Decision
Section titled “Decision”- The convention loop also rewrites every unique index an aggregate root declares so that
TenantIdleads it, under the database name the index had (the default one included), with its annotations kept and any per-column setting (sort order, collation, operator class, null order) moved along with its column. A configuration still names no tenant, andUniqueConstraintErrorsandUniqueConstraintTests.FaultOnPurposekey on the same name whichever way the toggle is set. - Identity's tables are not aggregates and keep their global unique indexes: one e-mail is one user in one tenant (decision 1).
TenancyTestsholds each of these: every unique index and every alternate key on an aggregate root starts withTenantId, in the real model and in a probe aggregate's (a plain index, one sorted descending on every column and one with a pinned name and per-column settings, built by the realAppDbContext), with a second probe holding a key in each shape the convention leaves global as the check's control; no other entity type mapped to a root's table (a value mapped withOwnsOne) declares a unique index or holds an alternate key, a probe root that owns such a value being that check's control; and no unique index outside the aggregates includesTenantIdin any place, a per-tenant index on the users being that check's control.
Consequences
Section titled “Consequences”- Two tenants may hold the same business key. A key that must be unique across every tenant (a public handle) is a deliberate change to the convention loop, like a shared aggregate.
- A business key is declared as a unique index (
.claude/rules/persistence.md), and another aggregate is referenced by its id: an alternate key, declared withHasAlternateKeyor made byHasPrincipalKey, is not rewritten, andTenancyTestsfails naming it. A value that needs a unique key is a converted column on the root (ADR-0006), not an owned type:OwnsOnemaps the value into the root's table as an entity type of its own, which the loop does not visit, so a unique index or key declared on it would be unique across every tenant on the root's own table, andTenancyTestsfails naming that as well. - A unique constraint the convention does not reach (a child table's unique index or alternate key that its root's id does not lead, a primary key a request chooses) stays unique across every tenant, and its 409, or the 500 of a fault on purpose, tells the caller that another tenant holds the value: ids are minted on the server and a business key is a unique index on the root, or the threat model's multi-tenancy gap records why a key stays global.
- A row written with no tenant is outside every tenant-led unique index. The
TenantIdcolumn is nullable andTenantInterceptorstamps nothing when there is no tenant (a job that did not setAmbientTenant, an anonymous endpoint, a webhook), and PostgreSQL treats NULLs as distinct, so two tenant-less rows may hold the same key, which the index refused while it was global. A flow that writes an aggregate is given its tenant before it writes; the threat model's multi-tenancy gap records it. - A unique index on exactly a foreign key's columns is the one EF keeps for a one-to-one reference:
replaced, EF adds its own again, global, under the name with a
1appended, andTenancyTestsfails naming it. The template's aggregates hold no such reference to each other; adding one means teaching the convention loop to leave that index as EF made it. - The toggle-off model, and so the shipped
InitialCreate, is unchanged; the regenerated multi-tenantInitialCreatecarries(TenantId, key)for each such index.
The documentation of Trellis.Templates 0.2.0 · release notes