【发布时间】:2020-04-20 11:16:28
【问题描述】:
在the documentation 之后,我创建了我的自定义 AuthenticationStateProvider,如下所示:
public class ApiAuthStateProvider : AuthenticationStateProvider
{
private static AuthenticationState anonymousState = ?
private AuthenticationState _authState;
public ApiAuthStateProvider()
{
_authState = anonymousState;
}
public void SetAuthenticationState(AuthenticationState authState)
{
_authState = authState ?? anonymousState;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
return Task.FromResult(_authState);
}
}
问题是如何初始化匿名状态,使 _authState.User.Identity.IsAuthenticated 为 false。如文档中所示,以下将产生经过身份验证的用户:
private static AuthenticationState anonymousState =
new AuthenticationState(new ClaimsPrincipal(
new ClaimsIdentity(new Claim[] {}, "none")));
即使是以下导致认证的用户:
public class AnonymousIdentity : IIdentity
{
public string AuthenticationType => "none";
public bool IsAuthenticated => false;
public string Name => string.Empty;
}
private static AuthenticationState anonymousState;
static ApiAuthStateProvider()
{
var anonymousIdentity = new AnonymousIdentity();
var user = new ClaimsIdentity(anonymousIdentity);
anonymousState = new AuthenticationState(
new ClaimsPrincipal(user));
}
我在这里错过了什么?
【问题讨论】:
标签: asp.net-core blazor blazor-client-side asp.net-core-security