如何使用身份服务器3在两个MVC Web应用程序(均为混合客户端)之间实现单点注销?

时间:2018-09-24 05:36:34

标签: asp.net-mvc identityserver3

我有2个ASP.NET MVC应用程序,它们使用OpenID中间件UseOpenIdConnectAuthentication来对两者进行单点登录。 SSO登录正常。如果我从应用程序1登录,然后检查是否也登录到连接到身份服务器的应用程序2,我可以看到自己已登录。

两个应用程序在Startup.cs中的配置相同,如下所述:

$rootScope.$watch

身份服务器中的客户端配置如下所述:

UPDATE TABLE2 
SET base2 = base2 + (SELECT * 
                     FROM INTERMEDIATEVALUE 
                     WHERE loadingordinal=counter2 AND itemid=counter1) 
WHERE loadingordinal=counter2 +1 AND itemid=counter1

使用以下代码完成注销:

        public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies"
        });


        AntiForgeryConfig.UniqueClaimTypeIdentifier = "sub";
        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

        app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = ClientId,
            //hybridclient1
            Authority = IdServBaseUri,
            RedirectUri = ClientUri,
            PostLogoutRedirectUri = ClientUri,
            ResponseType = "code id_token token",
            Scope = "openid profile email roles sampleApi offline_access",
            TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name",
                RoleClaimType = "role"
            },
            SignInAsAuthenticationType = "Cookies",
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthorizationCodeReceived = async n => {
                    var userInfoClient = new UserInfoClient(UserInfoEndpoint);
                    var userInfoResponse = await userInfoClient.GetAsync(n.ProtocolMessage.AccessToken);

                    var identity = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType);
                    identity.AddClaims(userInfoResponse.Claims);
                    //hybridclient1
                    var tokenClient = new TokenClient(TokenEndpoint, ClientId, SecretKey);
                    var response = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri);

                    identity.AddClaim(new Claim("access_token", response.AccessToken));
                    identity.AddClaim(new Claim("expires_at", DateTime.UtcNow.AddSeconds(response.ExpiresIn).ToLocalTime().ToString(CultureInfo.InvariantCulture)));
                    identity.AddClaim(new Claim("refresh_token", response.RefreshToken));
                    identity.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
                    identity.AddClaim(new Claim("sid", n.AuthenticationTicket.Identity.FindFirst("sid").Value));

                    n.AuthenticationTicket = new AuthenticationTicket(identity, n.AuthenticationTicket.Properties);

                },
                RedirectToIdentityProvider = n =>
                {
                    if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
                    {
                        var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
                        n.ProtocolMessage.IdTokenHint = idTokenHint;
                    }

                    return Task.FromResult(0);
                }
            }
        });




    }

当我尝试从app1注销时,它也使用IIS express从本地主机上的app2注销。但是我已经在IIS 8.5上分别托管了应用程序和身份服务器,并尝试实现相同的目的,但是当我从app1注销时,我并没有从app2注销。它将保持登录状态。

我也仔细阅读了@brockallen的有关单点退出的文章,以及有关identityserver3的github和stackoverflow上的其他一些类似问题,但是我没有得到答案。我可以通过为不同的域或任何其他方法设置不同的cookie来实现它吗?

如何使用身份服务器3实现不同域的单点注销?

0 个答案:

没有答案