【问题标题】:Blazor serverside + Cognito Logout?Blazor 服务器端 + Cognito 注销?
【发布时间】: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


    【解决方案1】:

    根据微软的文档AuthenticationStateProvider service,我们不应该直接使用AuthenticationStateProvider,因为它不会自动通知身份验证状态数据的变化。

    改用AuthrizedView 和CascadingStateProvider 组件。

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2020-05-15
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多