我创建了一个习惯ApiAuthenticationStateProvider
,在返回后AuthenticationState
仍然声明
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed.
这是我ApiAuthenticationStateProvider
失败的简化版本:
public class ApiAuthenticationStateProvider : AuthenticationStateProvider
{
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
Console.WriteLine("Getting auth state...");
var claims = new[] { new Claim(ClaimTypes.Name, "some.email@somewhere.com") };
var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(claims));
var authState = Task.FromResult(new AuthenticationState(authenticatedUser));
return Task.FromResult(authState);
}
}
我可以从中Console.WriteLine
看出正在使用我的自定义提供程序,但要提供完整的详细信息,这是我用来添加它的代码Program.cs
:
builder.Services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();