Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-029 — Session lifetime and lockout per tenant

Date: 2026-11-19 Status: Accepted Extends: ADR-023 (hierarchical settings), ADR-026 (a security policy per tenant, set by the system administrator). Origin: Azure DevOps US 10335; plan docs/plans/2026-11-19-feat-sessao-e-bloqueio-por-tenant-plano.md.

Context​

How long a session lasts and how many wrong passwords lock an account were one policy for the whole deployment: Authentication:Token:RefreshTokenLifetimeDays (14) in the appsettings, an 8-hour sliding cookie (ExpireTimeSpan) and IdentityOptions.Lockout (5 attempts, 15 minutes) in code. Each customer has its own security policy, and one tenant's stricter rule could not be met without imposing it on all the others.

Two facts shaped the decision. A wrong password is counted before there is a tenant: CheckPasswordAsync calls AccessFailedAsync and only then does IActiveTenantSelector pick the tenant; a tenant exists before the password only when domain resolution (ADR-011, opt-in) is on. The counter and the lock belong to the account, and the account opens every tenant it is a member of. The DevPack MFA counts a wrong second-factor code through the same UserManager.AccessFailedAsync.

Decision​

Four settings in SessionSettingDefinitions (levels Global and Tenant, EditableBy = System):

KeyGroupDefaultRangeConfiguration layer
Session.RefreshTokenLifetimeDaysauth.session141–90Authentication:Token:RefreshTokenLifetimeDays
Session.IdleTimeoutMinutesauth.session48015–1440—
Lockout.MaxFailedAttemptsauth.lockout53–20—
Lockout.DurationMinutesauth.lockout151–1440—

The defaults are the previous values, so a deployment without overrides behaves as before. ISessionPolicyProvider reads them with an explicit SettingContext (tenant or Global), since sign-in and a wrong password have no ambient tenant.

  • Refresh token: TokenIssuer stores ExpiresAt = now + lifetime of the tenant the pair is for, at issue, rotation and tenant switch.
  • Cookie session: SignInSessionAsync sets the ticket's IssuedUtc/ExpiresUtc from the tenant's idle window (TenantSessionWindow). The cookie handler slides a ticket by its own ExpiresUtc - IssuedUtc, so a session takes the target tenant's window on a switch. Since that span would otherwise stay with the ticket forever, the sliding check (FollowTenantIdleWindow) compares it with the tenant's current window on every use and stamps the new one when they differ. ExpireTimeSpan is read from the definition's default, so the number lives in one place.
  • Lockout: AppUserManager overrides AccessFailedAsync with the base algorithm and two numbers from the account's policy. That covers the password and the second-factor code. IdentityOptions.Lockout is no longer set. The policy is read before the counter moves. A save refused for a stale ConcurrencyStamp (two failures of the same account at once) is counted again on reloaded values, up to three times, so a parallel burst cannot slip under the threshold. IsLockedOutAsync is overridden too, so the lock is written and checked on the same TimeProvider.
  • An account in several tenants is locked by the strictest policy among its active tenants, each dimension on its own: the fewest attempts and the longest lockout (LockoutPolicy.Strictest). An account without an active membership uses Global. Domain resolution does not change the rule.
  • A new value applies from the next use, never by ending a session. A stored refresh token keeps its ExpiresAt and the next rotation uses the new value. An open cookie takes the new window on its next request, so the window counts from that request; a session idle longer than the new window but shorter than the old one gets that one request. A relaxed lockout does not unlock an account already locked.

Stays deployment-wide: the access token lifetime (one issuer, 15 minutes) and Authentication:Session:RevalidateMinutes, which is how fast a deactivation reaches an open cookie, not how long the session lasts.

Consequences​

  • The system administrator sets each customer's policy in Configurações do sistema with no new screen. The tenant's administrator sees the value and its origin but cannot change it.
  • A strict tenant tightens the lockout for the other tenants of the accounts it shares. This is accepted: the lockout protects the account, and the account is the same in every tenant.
  • An inactive tenant does not count, because its policy protects nothing that can be entered.
  • A RefreshTokenLifetimeDays in the appsettings outside 1–90 is now an invalid configuration layer and falls back to the default (InvalidStoredValue = FallBackToNextLayer).
  • Each session issue and each authenticated cookie request reads one cached setting. A failed password reads the account's active memberships and their policies, on a rare path that is already rate limited.

Alternatives considered​

  • The tenant of the sign-in decides the lockout. It does not exist when the password is wrong unless domain resolution is on, which would give two behaviours. Where it does exist, the caller picks the host, and so picks the most lenient tenant to get more guesses against an account a stricter tenant protects.
  • Retroactive cut (end a session already past the new value). Rejected: it ends active sessions by surprise. Deactivating the account remains the immediate cut.
  • Keep the cookie's window until it is reissued. Rejected in review: a session used at least once per window keeps the old one forever, so lowering the window after an incident would never reach the users who are signed in.
  • An absolute session ceiling (end the session from opened_at even with continuous use). Out of scope. The stamp exists (StampOpenedAt) if a later story asks for it.
  • Moving the definitions to the DevPack. The strictest rule reads the template's TenantUser, and the cookie is wired in the host. They are candidates to move up once a second consumer needs them.