In a Blazor Web App with WebAssembly interactivity, what do AddAuthenticationStateSerialization and AddAuthenticationStateDeserialization do, and what are their limits?
answer
- PersistentComponentState carries the user
- only name and role claims by default
- SerializeAllClaims or SerializationCallback
- fixed for the WebAssembly app's lifetime
- a display copy, not a credential
basics
~20 sAddAuthenticationStateSerialization persists the server's authentication state into the prerendered page; AddAuthenticationStateDeserialization registers a client AuthenticationStateProvider that reads it back. The copy holds name and role claims by default, never changes until a full reload, and authenticates nothing.
solid answer
~40 sAdded in .NET 9, the pair replaces the hand-written persisting providers of the .NET 8 template. On the server, `AddRazorComponents().AddInteractiveWebAssemblyComponents().AddAuthenticationStateSerialization()` registers a serializer that writes the user into `PersistentComponentState` for the WebAssembly side of the page. By default only the name claim and role claims are written; set `SerializeAllClaims = true` or replace `SerializationCallback` to change that. An anonymous user writes nothing. In the `.Client` project, `AddAuthorizationCore()`, `AddCascadingAuthenticationState()` and `AddAuthenticationStateDeserialization()` register a provider that reads the persisted data once and returns it for the app's lifetime; it never raises a change, so sign-in or sign-out needs a full page load. The data is for display and UI decisions only: calls to the server's APIs are authenticated by the cookie, and those APIs must authorize every request.
go deeper
Recall that the server call persists the user into the page and the client call reads it back, so AuthorizeView works in WebAssembly components.
Explain PersistentComponentState as the carrier, the name-and-role default, SerializeAllClaims, and why sign-in and sign-out need a full page load.
Show you keep the client copy to display decisions, secure every API on the server, and avoid serializing claims users should not see.
Discuss what identity data belongs in the browser at all, and how the team handles revocation for long-lived WebAssembly sessions.
## The problem In a Blazor Web App, a component with **Interactive WebAssembly** (or Interactive Auto) rendering is prerendered on the server and then runs in the browser. On the server the user is known from `HttpContext.User`. In the browser there is no `HttpContext` and no server-side `AuthenticationStateProvider`, yet `AuthorizeView` and `[CascadingParameter] Task<AuthenticationState>` must still work. Something has to carry the user across. ## The two registrations | Project | Call | Package | What it registers | |---|---|---|---| | server | `.AddAuthenticationStateSerialization()` on the Razor components builder | `Microsoft.AspNetCore.Components.WebAssembly.Server` | a scoped serializer that persists the current `AuthenticationState` | | `.Client` | `builder.Services.AddAuthenticationStateDeserialization()` | `Microsoft.AspNetCore.Components.WebAssembly.Authentication` | a scoped `AuthenticationStateProvider` that reads it back | The Blazor Web App template with Individual Accounts and WebAssembly or Auto interactivity writes both. Its `.Client` `Program.cs` also calls `AddAuthorizationCore()` and `AddCascadingAuthenticationState()`, because the browser needs the authorization services and the cascading value as well. ## How the data travels 1. While the server renders the page, the serializer is handed the current authentication state. 2. It registers a callback with **`PersistentComponentState`** for the **InteractiveWebAssembly** render mode only, so pages without WebAssembly-rendered components carry nothing. 3. When state is persisted, the callback converts the user to `AuthenticationStateData` and stores it as JSON in the page under an internal key. 4. In the browser, the deserializing provider reads that key once, in its constructor, and builds a `ClaimsPrincipal` from it. 5. `GetAuthenticationStateAsync` returns that same task for the rest of the WebAssembly app's lifetime. ## What is serialized - **Default:** the identity's name claim type and role claim type, the name claim, and every role claim. Enough for `@context.User.Identity?.Name` and `AuthorizeView Roles="Captain"`. - **`SerializeAllClaims = true`:** every claim, for apps whose policies read other claims, such as a membership tier in the rowing-club portal. - **`SerializationCallback`:** a `Func<AuthenticationState, ValueTask<AuthenticationStateData?>>` that replaces the default conversion entirely; the client has a matching `DeserializationCallback` on `AuthenticationStateDeserializationOptions`. - **Anonymous users:** nothing is written, and the client provider falls back to an unauthenticated principal. Serialized claims end up in the page source. Do not serialize anything a user should not see, such as internal identifiers or tokens. ```csharp builder.Services.AddRazorComponents() .AddInteractiveWebAssemblyComponents() .AddAuthenticationStateSerialization( options => options.SerializeAllClaims = true); ``` ## The limits - **Fixed for the app's lifetime.** The client provider never calls `NotifyAuthenticationStateChanged`. Signing in or out must end with a full page load (`NavigateTo(..., forceLoad: true)` or a form post to an endpoint) so a new page carries new state. - **Not a credential.** The client copy proves nothing to the server. The WebAssembly code calls the server's web APIs with the authentication cookie, and those endpoints must carry their own `RequireAuthorization` or `[Authorize]`. - **Modifiable.** Like all client-side code, the deserialized user can be altered in the browser. Use it to decide what to show, never to decide what is allowed. - **No revalidation.** A revalidating provider only runs in server circuits; a revoked role persists in a WebAssembly tab until the next full load. ## Interactive Auto: two providers for one component With **Interactive Auto**, the same component first runs on the server over a circuit, where the server's `AuthenticationStateProvider` supplies the user, and on later visits runs in the browser, where the deserialized provider does. The component code is identical, but: - server-side it can call services directly, and the user is the circuit's; - client-side it must call a web API, and the user is the serialized copy; - authorization therefore lives in two places: `[Authorize]` on the page for server rendering, and authorization on every API endpoint for client rendering. An interviewer who asks about Auto mode usually wants to hear that the second place is the one teams forget. ## Before .NET 9 .NET 8 templates generated the same flow as source code: a server-side `PersistingServerAuthenticationStateProvider` or `PersistingRevalidatingAuthenticationStateProvider` and a client-side `PersistentAuthenticationStateProvider`. Apps upgraded from .NET 8 can delete those classes and call the two framework methods.
- A WebAssembly component's AuthorizeView ignores a policy that reads a membership-tier claim. Why?By default the serializer writes only the name and role claims, so the tier claim never reaches the browser and the policy fails there. Set `SerializeAllClaims = true` in `AddAuthenticationStateSerialization`, or write a `SerializationCallback` that adds just the claims the client policies need.
- After signing in through a form, the WebAssembly part of the page still shows the member as anonymous. What is missing?The deserialized state is read once and never changes, so the WebAssembly app keeps the anonymous principal it started with. The sign-in flow must finish with a full page load, for example a redirect after the form post or `NavigateTo(url, forceLoad: true)`, so the server persists the new state into a fresh page.
- The deserialized client state authenticates the WebAssembly app's API calls
- All of the user's claims are serialized to the browser by default
- The client provider updates itself when the user signs out on the server
- Serialized auth state is written on every page, even without WebAssembly components
- AddAuthenticationStateDeserialization is called in the server project's Program.cs
Assumes ASP.NET Core 10: AddAuthenticationStateSerialization and AddAuthenticationStateDeserialization were added in .NET 9; .NET 8 templates generated PersistingServerAuthenticationStateProvider and PersistentAuthenticationStateProvider classes instead.