Audience: New engineers ramping into the auth/tenancy/authorization area. Goal: understand the layer every request passes through before touching it.
Three questions, every request:
IdentityService mints a JWT at login.CustomContextAccessorService decrypts the tenant scope from the JWT into a per-request ContextAccessorModel.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.
Read in this order:
AuthenticationModels.cs — ClaimParams, SignInStatusParams, SignInContextParams. The vocabulary of the whole layer.IdentityService.Authenticate — the login switch (password → status → token). Trace the branches to each SignInStatus.JwtService.GenerateTokens + GetClaimValue — how the token is built and read; note HmacSha256 + encryptionKey doubling as the claim-encryption key.CustomContextAccessorService.GetCredentials — the tenancy resolver. Note the context ∈ {Owned, Acquired} gate on tenant claims and the Decrypt(...) on org/company ids.IdentityModel.cs — the ~26 AdminRole tiers (+ int IDs) and the 199 Permission constants.RequireRoutePermit.tsx → usePermissions.tsx → permissions.ts. See the Primary/Secondary bypass in usePermissions.AdminRolesService — custom-role CRUD (the alias-joined bundle).| 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 |