【问题标题】:How to enable front-channel or back-channel logout in identityserver4如何在 identityserver4 中启用前通道或后通道注销
【发布时间】:2017-12-03 18:00:38
【问题描述】:

我正在研究当用户在身份服务器的部署上执行注销时(例如http://localhost:5000),如何断开当前登录 mvc 客户端的用户(例如http://localhost:5001

我知道在 identityserver4 中有一个 OAuth2 的实现可以做到这一点(https://openid.net/specs/openid-connect-backchannel-1_0.htmlhttps://openid.net/specs/openid-connect-frontchannel-1_0.html

对我来说幸运的是,Brock Allen 在不到一天前刚刚对样本进行了更改:https://github.com/IdentityServer/IdentityServer4.Samples/issues/197

但是,此时示例要么不完整,要么我遗漏了一些东西。

在我的服务器上,我将 FrontChannelLogoutUrl 的值设置为 http://localhost:5001/frontchannello,并将那段代码添加到我的 mvc 客户端(基本上是从示例中窃取的):

[HttpGet("frontChannello")]
public 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 new SignOutResult(new[] { "Cookies", "oidc" });
        }
    }

    return NoContent();
}

该代码永远不会被调用。

所以我的问题是:我应该使用反向通道还是前端通道?以及如何实现它

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    Identity server 4 documentation 很好地描述了应该如何实现前通道注销。查找 Quickstart 8_AspnetIdentity,因为它提供了实现所需的大部分代码。

    身份服务器所需代码的一些亮点:

    AccountController.cs 中,Logout 函数构建一个LoggedOutViewModel 并返回一个LoggedOut 视图。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Logout(LogoutInputModel model)
    {
       // build a model so the logged out page knows what to display
       var vm = await BuildLoggedOutViewModelAsync(model.LogoutId);
       ...
       return View("LoggedOut", vm);
    }
    

    SignOutIframeUrl iframe 在LoggedOut.cshtml 中提供。

    @model LoggedOutViewModel
    
    <div class="page-header logged-out">
       <small>You are now logged out</small>
       ...
       @if (Model.SignOutIframeUrl != null)
       {
           <iframe width="0" height="0" class="signout" src="@Model.SignOutIframeUrl"></iframe>
       }
    </div>
    

    剩下要做的是为您的每个客户定义FrontChannelLogoutUri。这通常在身份服务器的config.cs 中完成

     public static IEnumerable<Client> GetClients()
     {
            return new List<Client>
            {
                // resource owner password grant client
                new Client
                {
                    ClientId = "js",
                    ClientName = "JavaScript Client",
                    AllowedGrantTypes = GrantTypes.Code,
                    RequirePkce = true,
                    RequireClientSecret = false,
    
                    RedirectUris =           { "http://localhost:5003/callback.html" },
                    PostLogoutRedirectUris = { "http://localhost:5003/index.html" },
                    FrontChannelLogoutUri = "http://localhost:5003/frontChannello"
    

    【讨论】:

    • 这主要是可行的,但它在我刚刚注销的应用程序上调用注销,而不是其他客户端。仍在试图找出原因
    • @DaImTo 根据他们的规范,您的应用在技术上不应执行注销,您的应用应该只重定向到 IS 的结束会话端点,然后如果您添加了它会提示用户注销id_token 提示它可以自动注销。 IS 跟踪用户导航到的应用程序,它将为每个为用户导航到的客户端配置的前通道logoutIURi 创建一个 iframe,对于反向通道也是如此。所以 Is 将为您触发您的注销端点。阅读底部docs.identityserver.io/en/latest/topics/signout.html
    • 上面链接的 Aspnetidentity 快速入门已移至:github.com/IdentityServer/IdentityServer4/tree/main/samples/…
    【解决方案2】:

    好吧,很简单。在帐户控制器(在 idserver 中)上的 Logout 操作中,确保显示 LoggedOut 视图,该视图依次显示调用 mvc 客户端回调的 iFrame。与规范所说的差不多。

    【讨论】:

    • 但是这只给出了 一个 URL(并且该值甚至不来自FrontChannelLogoutUriBackChannelLogoutUri)。因此,它只能注销一个客户——而不是所有客户。
    猜你喜欢
    • 2019-02-08
    • 2018-10-13
    • 1970-01-01
    • 2018-07-18
    • 2021-04-11
    • 1970-01-01
    • 2016-04-18
    • 2022-12-29
    • 1970-01-01
    相关资源
    最近更新 更多