Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Sign-in across render modes

Signing in sets a cookie, and the server reads it on every request. An interactive page keeps running between requests, so it has to be told who the user is when it starts.

Sign-in pages are static

Setting a cookie needs an HTTP response, and only a static page or an endpoint has one. So the pages that sign in, register and sign out stay static, even in an app that is interactive everywhere else. The starter marks its account pages. Created with --all-interactive, its App.razor leaves the render mode off for them:

Components/Account/Pages/_Imports.razor
@attribute [ExcludeFromInteractiveRouting]
Components/App.razor
<Routes @rendermode="PageRenderMode" />

@code {
    [CascadingParameter]
    private HttpContext HttpContext { get; set; } = default!;

    // Every page is interactive except those marked [ExcludeFromInteractiveRouting].
    private IComponentRenderMode? PageRenderMode =>
        HttpContext.AcceptsInteractiveRouting() ? InteractiveServer : null;
}

Signing out is a form that posts to an endpoint, for the same reason. See Microsoft's Static SSR pages in an interactive app (opens in a new tab).

How each render mode learns who the user is

Browser Sign-in cookie Page in the browser Server Static page, or a new circuit Prerendered HTML the cookie name and roles
The cookie reaches the server with each request, and a circuit reads it once, as it starts. A page in the browser gets the user's name and roles with its first HTML.
Render modeThe user comes fromHow long it's kept
StaticThe cookie, read on each requestOne request
Interactive ServerThe cookie, read when the circuit startsAs long as the circuit, checked again on a timer
WebAssembly, and Auto once it runs in the browserThe prerendered page, which contains the user's name and rolesUntil the next full page load
  • A circuit outlives a request. If a user signs out in another tab or loses a role, an open circuit still has the old user. The starter registers IdentityRevalidatingAuthenticationStateProvider, which checks the user's security stamp every 30 minutes and signs the circuit out if it changed. Shorten its RevalidationInterval if that's too long for your app.
  • The browser gets a copy, not the cookie. The server writes the user's name and roles into the prerendered page, and the client reads them back. Register both halves:
Registering the user's state
// Program.cs (server)
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents()
    .AddAuthenticationStateSerialization();
builder.Services.AddCascadingAuthenticationState();
// For the pages on a circuit:
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();

// Program.cs (client)
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddAuthenticationStateDeserialization();

Read the user the same way everywhere

AuthorizeView, [Authorize] and the cascading AuthenticationState work in every render mode, so a component that uses them can move between render modes:

razor
<AuthorizeView Policy="Sales">
    <Authorized>
        <Button Href="orders/new">New order</Button>
    </Authorized>
</AuthorizeView>

@code {
    [CascadingParameter]
    private Task<AuthenticationState>? State { get; set; }

    private async Task<string?> UserName() =>
        State is null ? null : (await State).User.Identity?.Name;
}

Don't read HttpContext for the user outside a static page. An interactive component has no current request, so it's null or out of date there.

What the server still checks

  • Pages. [Authorize] on a page stops it rendering for the wrong user, in every render mode. On static pages the server redirects to the sign-in page before the page renders. On interactive pages, AuthorizeRouteView in Routes shows the redirect or its "not authorized" content.
  • Endpoints and rows. In the browser, [Authorize] only hides the page, and the copy of the user there is for showing a name or hiding a link. The browser sends the cookie with calls to the app's own URL, and the endpoint decides. See Checks that move to the server.

Next steps

Connection lost

Rejoining the server…