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:
[ExcludeFromInteractiveRouting]<Routes @rendermode="PageRenderMode" />
{
[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
| Render mode | The user comes from | How long it's kept |
|---|---|---|
| Static | The cookie, read on each request | One request |
| Interactive Server | The cookie, read when the circuit starts | As long as the circuit, checked again on a timer |
| WebAssembly, and Auto once it runs in the browser | The prerendered page, which contains the user's name and roles | Until 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 itsRevalidationIntervalif 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:
// 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:
<AuthorizeView Policy="Sales">
<Authorized>
<Button Href="orders/new">New order</Button>
</Authorized>
</AuthorizeView>
{
[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,AuthorizeRouteViewinRoutesshows 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
- Create an app:
--auth Individualadds these pages and registrations to the starter. - Blazor authentication and authorization (opens in a new tab): Microsoft's guide to the pieces named here.
- Data access by render mode: the stores and endpoints these checks protect.