|
1 | | -Blazor migration guidance will appear here prior to the release of .NET 9, which is scheduled for November, 2024. |
| 1 | +### Adopt simplified authentication state serialization for Blazor Web Apps |
| 2 | + |
| 3 | +Blazor Web Apps can optionally adopt [simplified authentication state serialization](xref:aspnetcore-9#simplified-authentication-state-serialization-for-blazor-web-apps). |
| 4 | + |
| 5 | +In the server project: |
| 6 | + |
| 7 | +* Remove the Persisting Authentication State Provider (`PersistingAuthenticationStateProvider.cs`). |
| 8 | + |
| 9 | +* Remove the service registration from the `Program` file. Instead, chain a call to <xref:Microsoft.Extensions.DependencyInjection.WebAssemblyRazorComponentsBuilderExtensions.AddAuthenticationStateSerialization%2A> on <xref:Microsoft.Extensions.DependencyInjection.RazorComponentsServiceCollectionExtensions.AddRazorComponents%2A>: |
| 10 | + |
| 11 | + ```diff |
| 12 | + - builder.Services.AddScoped<AuthenticationStateProvider, PersistingAuthenticationStateProvider>(); |
| 13 | + |
| 14 | + builder.Services.AddRazorComponents() |
| 15 | + .AddInteractiveServerComponents() |
| 16 | + .AddInteractiveWebAssemblyComponents() |
| 17 | + + .AddAuthenticationStateSerialization(); |
| 18 | + ``` |
| 19 | + |
| 20 | +The API only serializes the server-side name and role claims for access in the browser. To include all claims, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Server.AuthenticationStateSerializationOptions.SerializeAllClaims> to `true`: |
| 21 | + |
| 22 | +```csharp |
| 23 | +.AddAuthenticationStateSerialization(options => options.SerializeAllClaims = true); |
| 24 | +``` |
| 25 | + |
| 26 | +In the client project (`.Client`): |
| 27 | + |
| 28 | +* Remove the Persistent Authentication State Provider (`PersistentAuthenticationStateProvider.cs`). |
| 29 | + |
| 30 | +* Remove the service registration from the `Program` file. Instead, call <xref:Microsoft.Extensions.DependencyInjection.WebAssemblyAuthenticationServiceCollectionExtensions.AddAuthenticationStateDeserialization%2A> on the service collection: |
| 31 | + |
| 32 | + ```diff |
| 33 | + - builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>(); |
| 34 | + + builder.Services.AddAuthenticationStateDeserialization(); |
| 35 | + ``` |
| 36 | + |
| 37 | +For more information, see <xref:aspnetcore-9#simplified-authentication-state-serialization-for-blazor-web-apps>. |
0 commit comments