skip to content

In an Interactive Server Blazor app, why can a member whose Captain role was revoked keep seeing admin UI, and how does RevalidatingServerAuthenticationStateProvider address it?

level: seniorimportance: should knowfreq 34%

answer

  1. the user is fixed at circuit start
  2. no cookies on in-circuit navigation
  3. RevalidationInterval and ValidateAuthenticationStateAsync
  4. template: security stamp every 30 minutes
  5. false means anonymous user

basics

~20 s

A circuit keeps the user from the connection that started it, and in-circuit navigation sends no cookies, so a revoked role lingers. RevalidatingServerAuthenticationStateProvider re-checks the user every RevalidationInterval and swaps in an anonymous user when validation fails.

solid answer

~50 s

An Interactive Server circuit gets its `ClaimsPrincipal` when the SignalR connection is established and keeps it for the connection's lifetime; it is re-evaluated on reconnection. Navigation inside the circuit makes no HTTP request, so the authentication cookie is never re-read and a role removed in the database stays in the claims. `RevalidatingServerAuthenticationStateProvider` is the built-in fix: derive from it, override `RevalidationInterval` and `ValidateAuthenticationStateAsync`, and register it as the scoped `AuthenticationStateProvider`. For an authenticated user it loops, waiting the interval and validating; on `false` or an exception it sets an anonymous user, and the cascading state notifies `AuthorizeView` and `AuthorizeRouteView`. The Identity template's `IdentityRevalidatingAuthenticationStateProvider` checks the security stamp every 30 minutes, so revoking a role should also update the stamp. It signs out but does not grant new roles; for immediate effect, also check permissions in the operation itself.

go deeper

for a junior

Recall that an Interactive Server circuit keeps the user it started with, and that a revalidating provider re-checks the user on an interval.

for a middle

Explain why no cookie is read during in-circuit navigation, the two members a revalidating provider overrides, and the template's 30-minute security stamp check.

for a senior

Show you can size the revalidation interval against database cost, scope dependencies correctly, and still enforce rights inside each operation.

for a principal

Discuss the acceptable window between revoking access and losing it, and how session revocation is designed across circuits, cookies and APIs.

## Where a circuit's user comes from With **Interactive Server** rendering, a component runs on the server inside a **circuit**, a per-tab session carried over a SignalR connection. When that connection is established, the framework copies the connection's `ClaimsPrincipal` into the circuit's `AuthenticationStateProvider`. From then on: - navigating between pages happens over the connection, so **no HTTP request is made and no cookie is read**; - the user is kept for the lifetime of the connection and re-evaluated only when the client reconnects; - `AuthorizeView` and `AuthorizeRouteView` evaluate roles and policies against that stored principal. In the rowing-club portal, a captain's role is removed in the database at 10:00. Her open tab still holds the claims from the morning, so the **Crew roster** menu and the `[Authorize(Roles = "Captain")]` pages keep working for her until something changes the circuit's user. ## `RevalidatingServerAuthenticationStateProvider` The server package ships an abstract base class for this case. A subclass supplies two members: | Member | Purpose | |---|---| | `protected abstract TimeSpan RevalidationInterval { get; }` | how long to wait between checks | | `protected abstract Task<bool> ValidateAuthenticationStateAsync(AuthenticationState state, CancellationToken ct)` | return `false` when the user is no longer valid | Its loop works as follows: 1. When an authenticated state is set, it starts a loop for that state. 2. Each iteration waits `RevalidationInterval`, then calls `ValidateAuthenticationStateAsync`. 3. If the method returns `false`, or throws, the provider replaces the state with an **anonymous user** (an empty `ClaimsIdentity`) and stops. An exception is also logged. 4. Setting a new state or disposing the provider (when the circuit ends) cancels the loop. Because replacing the state raises `AuthenticationStateChanged`, the root-level cascading value registered by `AddCascadingAuthenticationState()` pushes the anonymous user to every component. Menus hidden by `AuthorizeView` disappear, and `AuthorizeRouteView` renders its `NotAuthorized` content for the current protected page. ## What the template does The Blazor Web App template with Individual Accounts registers `IdentityRevalidatingAuthenticationStateProvider` as the scoped `AuthenticationStateProvider`. It derives from the base class with `RevalidationInterval => TimeSpan.FromMinutes(30)` and validates by comparing the user's **security stamp** claim with the stamp stored by ASP.NET Core Identity. It also fails when the user no longer exists. Identity changes the stamp on events such as a password change, but `UserManager.RemoveFromRoleAsync` does not touch it, so revoking a role should be followed by `UserManager.UpdateSecurityStampAsync`; otherwise the template's check keeps passing. ```csharp internal sealed class ClubRevalidatingStateProvider( ILoggerFactory loggerFactory, IServiceScopeFactory scopeFactory) : RevalidatingServerAuthenticationStateProvider(loggerFactory) { protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(5); protected override async Task<bool> ValidateAuthenticationStateAsync( AuthenticationState state, CancellationToken ct) { await using var scope = scopeFactory.CreateAsyncScope(); var members = scope.ServiceProvider.GetRequiredService<MemberDirectory>(); return await members.IsStillActiveAsync(state.User, ct); } } ``` Creating a scope inside the check matters: the provider lives as long as the circuit, so it must not hold a scoped database context for hours. ## Limits of revalidation - **It only signs out.** It does not refresh claims or add new roles; a promoted member needs a new circuit (a full reload) to see new rights. - **The interval is a window.** With 30 minutes, a revoked captain keeps access for up to 30 minutes. Shorter intervals cost a query per circuit per interval. - **The cookie is separate.** Revalidation changes the circuit's user; the browser's cookie stays until it expires or the user signs out through an endpoint. - **It is server only.** WebAssembly components get a fixed, serialized state and need full reloads or API-side checks. ## Reconnection and reloads - **Reconnection** after a dropped connection re-evaluates the user from the new connection. - **A full page reload** starts a new circuit, whose user comes from the request's authentication cookie. - **The cookie carries the claims issued at sign-in.** If the role was baked into the cookie, a reload alone does not remove it; when the cookie is validated and re-issued is decided by the cookie and Identity configuration, which the authentication topics cover. So revalidation in the circuit and validation of the cookie are two separate settings, and both shape how long a revoked captain keeps access. ## Defence in depth The dependable control is in the operation: the service that edits a crew checks the member's current rights in the database at the moment of the change. Revalidation then keeps the UI honest, and the operation check keeps the data safe in between.

  • What happens on screen when ValidateAuthenticationStateAsync returns false?
    The provider sets an anonymous `AuthenticationState` and raises `AuthenticationStateChanged`. The cascading authentication state notifies subscribers, so `AuthorizeView` switches to `NotAuthorized` content and `AuthorizeRouteView` stops rendering the current `[Authorize]` page, showing its `NotAuthorized` fragment, which in the template redirects to login with a full page load.
  • With the template's provider, a captain's role is removed with UserManager.RemoveFromRoleAsync and an hour later her tab still shows captain tools. Why?
    The template's provider only compares security stamps, and `RemoveFromRoleAsync` saves the user without changing the stamp, so validation keeps succeeding. Call `UserManager.UpdateSecurityStampAsync` after revoking the role; within one `RevalidationInterval` the check fails and the circuit switches to an anonymous user.
  • Why should ValidateAuthenticationStateAsync create its own DI scope?
    The provider is scoped to the circuit, which can live for hours. Capturing a scoped `DbContext` or user manager from the circuit's scope would keep one instance for the whole session; creating a short scope per check gets fresh instances and releases them afterwards.

A circuit's user is like a wristband issued at a regatta gate: once inside, nobody goes back to the ticket office. Revalidation is a marshal who walks the grounds on a schedule and cuts the wristbands of people struck off the list; the marshal never hands out new ones.

  • Every in-app navigation in a circuit re-reads the authentication cookie
  • RevalidatingServerAuthenticationStateProvider refreshes the user's claims with newly granted roles
  • A role removed in the database disappears from an open circuit immediately
  • Revalidation also runs in WebAssembly components that received serialized auth state
  • Hiding admin menus is enough once revalidation is configured