【问题标题】:AspCore Client Global Signout with IdentityServer使用 IdentityServer 的 Asp 核心客户端全局注销
【发布时间】:2017-03-02 20:54:38
【问题描述】:

在一个非核心 asp mvc 应用程序中,我有一个控制器操作用于注销用户全局

看起来像这样

   public ActionResult Logout()
    {
        Request.GetOwinContext().Authentication.SignOut();
        return Redirect("/");
    }

现在我有一个 ASP 核心客户端,想要注销我尝试过

 public async Task<ActionResult> LogOut()
        {
            if (User.Identity.IsAuthenticated)
            {
                await HttpContext.Authentication.SignOutAsync("Cookies");
            }
            return Redirect("/");
        }

更新

现在好像我退出了,但我被重定向到需要身份验证的站点。 我可以看到我很快又被重定向到身份服务器,它会自动再次向我唱歌。

总结: 我在我的 asp 应用程序中登出,但在身份服务器上没有登出。

如何全局注销?所以我需要在身份服务器上登录?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-core asp.net-identity identityserver4


    【解决方案1】:

    如果使用OpenIdConnect认证,还需要远程注销。尝试更改您的代码,如下所示:

        public async Task<ActionResult> LogOut()
        {
            if (User.Identity.IsAuthenticated)
            {
                await HttpContext.Authentication.SignOutAsync("Cookies");
                await context.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties()
                {
                     RedirectUri = "<signed out url>"
                });
    
                // if you use different scheme name for openid authentication, use below code
                //await context.Authentication.SignOutAsync(<your openid scheme>, new AuthenticationProperties()
                //{
                //   RedirectUri = "/signedout"
                //});
            }
            return Redirect("/");
        }
    

    查看原样https://github.com/aspnet/Security/blob/dev/samples/OpenIdConnectSample/Startup.cs

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,为了解决这个问题,我必须自己在 IdentityServer 应用程序中清除响应 cookie。

      var cookies = HttpContext.Request.Cookies.Keys;
      foreach (var cookie in cookies)
      {
          HttpContext.Response.Cookies.Delete(cookie, new CookieOptions
          {
              Domain = "localhost" // Your host name here
          });
      }
      
      // SignOutAsync depends on IdentityServer4 > Microsoft.AspNetCore.Http.Authentication
      await HttpContext.Authentication.SignOutAsync();
      

      我所做的是,当我的客户想要退出时,我将其重定向到我的 IdentityServer 应用程序,该应用程序如上所述清除响应 Cookie。

      在此代码中,我将删除 localhost 的所有 Cookie,但是您可以在此处添加过滤器并仅删除 IdentityServer 用来保持用户身份验证的 cookie。

      您将在下面找到有关此实现的更多详细信息。

      http://benjii.me/2016/04/single-sign-out-logout-identity-server-4/

      【讨论】:

        【解决方案3】:

        这是 ASP.NET Identity Server Single Sign-Out 的官方文档:

        single sign out documentation

        这是从他们的演示实现中提取的示例:

        public ActionResult Signout()
        {
            Request.GetOwinContext().Authentication.SignOut();
            return Redirect("/");
        }
        
        public void SignoutCleanup(string sid)
        {
            var cp = (ClaimsPrincipal)User;
            var sidClaim = cp.FindFirst("sid");
            if (sidClaim != null && sidClaim.Value == sid)
            {
                Request.GetOwinContext().Authentication.SignOut("Cookies");
            }
        }
        

        【讨论】:

        • 我想说它在这种情况下没有用,因为我们正在谈论 AspNetCore 应用程序并且我们正在使用托管代码中的 OWIN 库。但是它在使用 OWIN 的非 asp.net 核心应用程序中运行良好。
        猜你喜欢
        • 2016-11-26
        • 1970-01-01
        • 2019-03-17
        • 2018-02-28
        • 2011-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多