在Blazor WebAssembly客户端应用程序中使用显式获取身份验证状态的方法。以下示例演示如何在组件中使用AuthenticationStateProvider
来获取身份验证状态:
@inject AuthenticationStateProvider AuthenticationStateProvider
@code{
private AuthenticationState _authenticationState;
protected override async Task OnInitializedAsync()
{
_authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
}
}
在这个示例中,我们注入了AuthenticationStateProvider
并使用GetAuthenticationStateAsync
方法异步获取身份验证状态并保存在组件的私有字段中。您可以将此代码添加到您的组件中,以获取当前的身份验证状态并使用它来更新您的UI。