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
AuthenticationStateProvider
service. - 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
AuthorizeView
component andCascadingAuthenticationState
component to get the authentication state. - Don’t use
AuthenticationStateProvider
directly. Use theAuthorizeView
component. - The main drawback to using
AuthenticationStateProvider
directly 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