Blazor服务器端+Cognito注销



我对blazor有点陌生,在将用户从应用程序中注销时遇到了问题。我查看了各种文档和教程,但没有发现任何提及注销的内容。我已尝试调用Cognito注销端点(https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html)但是我的应用程序仍然将用户视为已通过身份验证。我还尝试使用此线程中的答案获取access_token。如何从blazor(服务器端(web应用程序获取访问令牌?但它对我来说总是返回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"];
});

在LogiDiskplay.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
);
}

根据Microsoft的文档AuthenticationStateProvider服务,我们不应该直接使用AuthenticationStateProvider,因为它不会自动通知身份验证状态数据更改。

请改用AuthrizedView和CascadingStateProvider组件。

相关内容

  • 没有找到相关文章

最新更新