【问题标题】:identity server 4 Logout from all clients using FrontChannelLogoutUri not working身份服务器 4 使用 FrontChannelLogoutUri 从所有客户端注销不起作用
【发布时间】:2021-02-18 07:28:24
【问题描述】:

这里我想实现SSO功能。如果我从身份服务器注销,则应注销使用该用户 ID 连接到该服务器的所有客户端。但它对我有用。

我正在使用身份服务器版本="4.0.0"

我已经设置了 IDS 客户端、用于注销的 UI 模板、MVC 客户端,如下所示。但这并不是所有客户端的注销。

new Client
    {
        ClientId = "testmvc",

        AllowedGrantTypes = GrantTypes.Code,
        
       
        // secret for authentication
        ClientSecrets =
        {
            new Secret("Secret".Sha256())
        },

        AllowOfflineAccess = true,

        // scopes that client has access to
        AllowedScopes = new List<string>
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            IdentityServerConstants.StandardScopes.Address,
            "roles"
        },

        // where to redirect to after login
        RedirectUris = { "https://localhost:5002/signin-oidc" },
        
        

        // where to redirect to after logout
        PostLogoutRedirectUris = { "https://localhost:5002/signout-callback-oidc" },
        

        FrontChannelLogoutUri  = "https://localhost:5002/home/frontchannellogout" ,
        
    }

在 MVC 客户端中,我创建了两个注销

//UI Logout
public async Task<IActionResult> Logout()
        {

            var client = _httpClientFactory.CreateClient("IDPClient");

            var discoveryDocumentResponse = await client.GetDiscoveryDocumentAsync();
            if (discoveryDocumentResponse.IsError)
            {
                throw new Exception(discoveryDocumentResponse.Error);
            }

            var accessTokenRevocationResponse = await client.RevokeTokenAsync(
                new TokenRevocationRequest
                {
                    Address = discoveryDocumentResponse.RevocationEndpoint,
                    ClientId = "testmvc",
                    ClientSecret = "Secret",
                    Token = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken)
                });

            if (accessTokenRevocationResponse.IsError)
            {
                throw new Exception(accessTokenRevocationResponse.Error);
            }
            
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
            
            return Redirect(discoveryDocumentResponse.EndSessionEndpoint);
        }

//Front channel logout
            public async Task<IActionResult> FrontChannelLogout(string sid)
            {
                if (User.Identity.IsAuthenticated)
                {
                    var currentSid = User.FindFirst("sid")?.Value ?? "";
                    if (string.Equals(currentSid, sid, StringComparison.Ordinal))
                    {
                        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                    }
    
                }
    
                return NoContent();
            }

【问题讨论】:

    标签: asp.net-core-mvc single-sign-on identityserver4 logout single-logout


    【解决方案1】:

    在Logout 方法中,您从两个身份验证方案(Cookie 和OIDC)中退出,而在FrontChannelLogout 中,您似乎只从Cookie 方案中退出。不是这个问题吗?

    无论哪种方式,请尝试定义如下方法,并检查是否调用了对应的OIDC注销端点。

    public async Task Logout()
    {
        // ...
        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
    }
    
    public async Task FrontChannelLogout(string sid)
    {
        if (User.Identity.IsAuthenticated)
        {
            //...
                await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
        }
    }
    

    【讨论】:

    • 我在下面的博客中使用了从所有客户端注销andersonnjen.com/2019/03/22/identityserver4-global-logout 在 UI Logout 中,它清除了两者,因此已经从 Identity Server 注销。但注意到 FrontChannelLogout() 没有被调用。所以这不是问题。
    • 好的,我刚才相应地更新了答案
    • 实际问题是没有调用 FrontChannelLogout。你能帮我确定它被调用的原因吗?
    • 我已上传身份服务器注销会话值。我修改了提到的函数,但 FrontChannelLogout 没有调用。请检查。
    • 我不确定我是否理解您的问题,但您的 FrontChannelLogout 操作必须由您的应用调用,例如使用退出按钮或类似的东西...您必须调用该操作明确地。然后,SignOutAsync 方法将根据您的配置处理注销(大概是您在 Startup 中设置的)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2022-01-01
    • 2018-10-26
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多