使用 CascadingAuthenticationState 组件将 AuthenticationState 对象向下传递给需要它的组件。具体方法如下:
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
@code {
private AuthenticationState authState;
protected override async Task OnInitializedAsync()
{
authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
}
}
这样就可以在组件中使用 AuthenticationState 对象了。当需要向下传递 AuthenticationState 对象时,只需要使用 CascadingAuthenticationState 组件即可。