
Implementing JWT authentication in standalone Blazor WASM requires a custom AuthenticationStateProvider that reads the token and extracts claims to populate the user principal.
public class JwtAuthStateProvider : AuthenticationStateProvider
{
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var token = await _storage.GetItemAsync<string>("auth_token");
if (string.IsNullOrEmpty(token))
return new AuthenticationState(new ClaimsPrincipal());
var claims = ParseClaimsFromJwt(token);
var identity = new ClaimsIdentity(claims, "jwt");
return new AuthenticationState(new ClaimsPrincipal(identity));
}
}builder.Services.AddScoped<AuthenticationStateProvider, JwtAuthStateProvider>();
builder.Services.AddAuthorizationCore();Reference:
TaskLoco™ — The Sticky Note GOAT