【发布时间】:2020-12-29 13:50:21
【问题描述】:
我对 blazor 有点陌生,我在将用户从应用程序中注销时遇到了问题。我查看了各种文档和教程,但没有发现任何提及注销的内容。我尝试调用 Cognito 注销端点 (https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html),但我的应用程序仍然认为用户已通过身份验证。我还尝试使用此线程How do I get the access token from a blazor (server-side) web app? 中的答案获取 access_token,但它总是为我返回 null。无论我做什么,isAuthenticated 属性总是返回 true。有人有什么想法吗?
在启动中
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ResponseType = "code";
options.SaveTokens = true;
options.RemoteSignOutPath = "/signout";
options.MetadataAddress = Configuration["Authentication:Cognito:MetadataAddress"];
options.ClientId = Configuration["Authentication:Cognito:ClientId"];
options.ClientSecret = Configuration["Authentication:Cognito:ClientSecret"];
});
在 LoginDisplay.razor 中
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
identity = user.Identity as ClaimsIdentity;
var isAuthenticated = identity.IsAuthenticated;
email = identity!.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value ?? string.Empty;
var userData = await userService.GetUserByEmail(email.ToLower());
userData.Then(u =>
userRole = u.Role
, () =>
userRole = UserRole.Anonymous
);
}
【问题讨论】:
标签: authentication .net-core amazon-cognito blazor blazor-server-side