【发布时间】:2020-10-29 01:12:53
【问题描述】:
我创建了一个自定义 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>();
【问题讨论】:
标签: .net-core blazor blazor-webassembly