Audience: New engineers ramping into the auth/tenancy/authorization area. Goal: understand the layer every request passes through before touching it.


Mental model (read this first)

Three questions, every request:

  1. Who are you? → IdentityService mints a JWT at login.
  2. Which company? → CustomContextAccessorService decrypts the tenant scope from the JWT into a per-request ContextAccessorModel.
  3. Allowed? → tier (coarse, roles claim + [Authorize]) + per-permission (fine, permissions claim + RequireRoutePermit).

Internalise the two-DB split and the admin bypass early — they cause most "why is this happening" moments.

The 30-minute code tour

Read in this order:

  1. AuthenticationModels.cs — ClaimParams, SignInStatusParams, SignInContextParams. The vocabulary of the whole layer.
  2. IdentityService.Authenticate — the login switch (password → status → token). Trace the branches to each SignInStatus.
  3. JwtService.GenerateTokens + GetClaimValue — how the token is built and read; note HmacSha256 + encryptionKey doubling as the claim-encryption key.
  4. CustomContextAccessorService.GetCredentials — the tenancy resolver. Note the context ∈ {Owned, Acquired} gate on tenant claims and the Decrypt(...) on org/company ids.
  5. IdentityModel.cs — the ~26 AdminRole tiers (+ int IDs) and the 199 Permission constants.
  6. Frontend: RequireRoutePermit.tsx → usePermissions.tsx → permissions.ts. See the Primary/Secondary bypass in usePermissions.
  7. AdminRolesService — custom-role CRUD (the alias-joined bundle).

Where things live

You want to change… Go to
Login / token / 2FA / SSO IdentityService.cs, AccountController.cs, IdentityController.cs
Tenant resolution CustomContextAccessorService.cs, TenantDbFactory.cs
Tiers / permission constants IdentityModel.cs (backend), permissions.ts (frontend)
Custom roles AdminRolesService.cs, Admin/RolesController.cs, settings/roles-permissions/*
Admin lists / primary-admin transfer MembershipService.cs, MembershipController.cs
Route guarding (FE) RequireRoutePermit.tsx, usePermissions.tsx
Session state (FE) stores/auth.ts

Things that will trip you up (read before your first PR)