具有 OpenId owin katana 中间件的多个 MVC 应用程序的单个注销 Identity Server3

Posted

技术标签:

【中文标题】具有 OpenId owin katana 中间件的多个 MVC 应用程序的单个注销 Identity Server3【英文标题】:Single Logout Identity Server3 for multiple MVC applications with OpenId owin katana middleware 【发布时间】:2017-01-22 23:00:51 【问题描述】:

我已经实现了 IdentityServer3 来创建我自己的身份服务器,该服务器托管在 azure 云上。 我有几个客户端应用程序通过我的身份服务器进行身份验证。这两个应用程序都是 MVC 应用程序。其中一个在 Azure 上作为 cloudapp 运行,另一个在我的本地计算机上并在 localhost 上运行。正如预期的那样,当我登录其中一个时,我会自动登录到另一个。 但是,当我退出其中任何一个时,我不会自动从在同一浏览器中打开的另一个退出。

任何帮助将不胜感激...:)

以下是我的客户端应用程序的身份验证配置

    public void ConfigureAuth(IAppBuilder app)
    
        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        
            AuthenticationType = "Cookies"
        );

        var options = new OpenIdConnectAuthenticationOptions
        
            ClientId = ConfigHelper.GetAppSetting(ConfigConstants.ClientIdKeyName),
            Authority = ConfigHelper.GetAppSetting(ConfigConstants.IdpUriKeyName),
            RedirectUri = "https://myclient1.cloudapp.net/",
            PostLogoutRedirectUri = "https://myclient1.cloudapp.net/account/logoutcallback",
            ResponseType = "code id_token token",
            Scope = "openid profile address roles email",

            TokenValidationParameters = new TokenValidationParameters
            
                NameClaimType = "name",
                RoleClaimType = "role"
            ,

            SignInAsAuthenticationType = "Cookies",

            Notifications = new OpenIdConnectAuthenticationNotifications
            
                AuthorizationCodeReceived = async n =>
                
                    var tokenClient = new TokenClient(
                        "https://myidp.cloudapp.net/core/connect/token",
                        "myclient-1",
                        "myclient-1-secret");

                    var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(
                        n.Code, n.RedirectUri);

                    if (tokenResponse.IsError)
                    
                        throw new Exception(tokenResponse.Error);
                    

                    var id = new ClaimsIdentity(n.AuthenticationTicket.Identity.Claims, n.AuthenticationTicket.Identity.AuthenticationType);
                    n.AuthenticationTicket = new AuthenticationTicket(new ClaimsIdentity(id.Claims, n.AuthenticationTicket.Identity.AuthenticationType, "name", "role"),
                    n.AuthenticationTicket.Properties);
                
            
        ;

        app.UseOpenIdConnectAuthentication(options);
    

我在打电话

this.Request.GetOwinContext().Authentication.SignOut();

注销身份服务器

当它回到我的客户端应用程序时,它涉及

    public ActionResult LogoutCallback()
    
        HttpCookie cookie = new HttpCookie("SecureCookieName");
        cookie.HttpOnly = true;
        cookie.Expires = new DateTime(1999, 10, 12);
        Response.Cookies.Remove("SecureCookieName");
        Response.Cookies.Add(cookie);
        SessionManager.KillSession();  //Custom stuff to clear the session of client application.
        return RedirectToAction("Index", "Home");
    

【问题讨论】:

您的 IdentityServer3 客户端配置是什么样的?具体来说,为您的客户设置了哪些 LogoutUri 值? 对不起..我的错..我忘了用实际值替换我的常量。我已经更新了帖子。请再看一遍。提前感谢您的帮助。 【参考方案1】:

一种解决方法:

在每个名为注销端点的每个客户端上创建一个属性,或者约定每个客户端都应该有一个 /account/logout/ 路由。

然后,在每个应用程序中实现此路由,并使该路由删除应用程序 cookie。在 idsrv 的 logout.html 视图中,创建一个循环遍历所有客户端列表并为每个客户端调用此端点的 javascript

【讨论】:

感谢约翰的想法。如果我们只有少数客户端,循环遍历所有客户端的注销端点确实有意义。如果列表增长,那么这不是一个可行的想法。就我而言,我将拥有 20 多个客户端应用程序。 好吧,这取决于你。这是说明它的文档。 identityserver.github.io/Documentation/docsv2/advanced/…

以上是关于具有 OpenId owin katana 中间件的多个 MVC 应用程序的单个注销 Identity Server3的主要内容,如果未能解决你的问题,请参考以下文章

OWIN 身份验证管道正确使用 Katana 中间件?

OWIN / Katana 的未处理异常全局处理程序?

OpenID Connect 的 OWIN 中间件 - 代码流(流类型 - AuthorizationCode)文档?

Azure OpenID Connect 通过 OWIN 中间件导致无限重定向循环

Owin/Katana - 与 ASP.NET 请求生命周期集成

身份验证/授权 MVC 5 和 Web API - Katana/Owin