AuthenticationStateProvider
[Inject]
public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();AuthenticationState
[CascadingParameter]
private Task<AuthenticationState> AuthenticationStateTask { get; set; }
var user = (await AuthenticationStateTask).User;AuthenticationStateProvider vs AuthenticationState

- Blazor has a built-in service called AuthenticationStateProviderservice.
- This service obtains authentication state data from ASP.NET Core’s HttpContext.User.
- This is how authentication state integrates with existing ASP.NET Core authentication mechanisms.
- It is this service that is used by AuthorizeViewcomponent andCascadingAuthenticationStatecomponent to get the authentication state.
- Don’t use AuthenticationStateProviderdirectly. Use theAuthorizeViewcomponent.
- The main drawback to using AuthenticationStateProviderdirectly is that the component isn’t notified automatically if the underlying authentication state data changes.
Sources:
https://www.pragimtech.com/blog/blazor/authorization-in-blazor/
Comments