What is the purpose of the AuthenticationStateProvider, and how do you customize it to handle JWT-based auth in a WASM app?
Last updated
AuthenticationStateProvidersupplies the current user'sClaimsPrincipaltoAuthorizeViewand[Authorize]viaGetAuthenticationStateAsync().For JWT, subclass it to read the token from storage and build a
ClaimsIdentitywith an auth type (soIsAuthenticatedis true) and correct role claims.Call
NotifyAuthenticationStateChangedon login/logout, register it in DI, and attach the token as aBearerheader (e.g. via aDelegatingHandler).
- https://learn.microsoft.com/en-us/aspnet/core/blazor/security/authentication-state?view=aspnetcore-10.0
- https://www.youtube.com/watch?v=iq2btD9WufI
- https://medium.com/@shahriyarali08/authentication-and-role-based-authorization-in-blazor-wasm-using-jwt-and-7bcc12317ea
- https://code-maze.com/authenticationstateprovider-blazor-webassembly/
- https://code-maze.com/blazor-webassembly-authentication-aspnetcore-identity/