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 authenticationstateprovider
  • 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 and CascadingAuthenticationState component to get the authentication state.
  • Don’t use AuthenticationStateProvider directly. Use the AuthorizeView 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/

Last modified: December 26, 2020

Author

Comments

Write a Reply or Comment