【问题标题】:OWIN SignOut doesn't remove cookieOWIN SignOut 不会删除 cookie
【发布时间】:2016-04-16 12:03:46
【问题描述】:

我在外部身份验证服务器中使用 OWIN 中间件,我的应用程序使用 OAuth 授权代码授予流程对其进行身份验证。

我可以重定向到身份验证服务器,针对外部提供商 (Google) 进行身份验证,并使用已登录的用户和应用程序 Cookie 设置重定向回我的客户端应用程序,但是当我尝试注销时,cookie 在我之后仍然存在调用AuthenticationManager.SignOut 方法。

Startup.Auth.cs 中我的 cookie 选项是:

var cookieOptions = new CookieAuthenticationOptions
                    {
                        Provider = cookieProvider,
                        AuthenticationType = "Application",
                        AuthenticationMode = AuthenticationMode.Passive,
                        LoginPath = new PathString("/Account/Index"),
                        LogoutPath = new PathString("/Account/Logout"),
                        SlidingExpiration = true,
                        ExpireTimeSpan = TimeSpan.FromMinutes(30),
                    };
app.UseCookieAuthentication(cookieOptions);
app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

我的登录方式:

var loginInfo = await AuthManager.GetExternalLoginInfoAsync();
SignInManager.ExternalSignInAsync(loginInfo, true);
var identity = AuthManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie).Result.Identity;

if (identity != null)
{
    AuthManager.SignIn(
                  new AuthenticationProperties {IsPersistent = true},
                  new ClaimsIdentity(identity.Claims, "Application", identity.NameClaimType, identity.RoleClaimType));

        var ticket = AuthManager.AuthenticateAsync("Application").Result;
        var identity = ticket != null ? ticket.Identity : null;
        if (identity == null)
        {
            AuthManager.Challenge("Application");
            return new HttpUnauthorizedResult();
        }

        identity = new ClaimsIdentity(identity.Claims, "Bearer", identity.NameClaimType, identity.RoleClaimType);
        AuthManager.SignIn(identity);
}

return Redirect(Request.QueryString["ReturnUrl"]);

退出方式:

var authTypeNames = new List<string>();
authTypeNames.Add("Google");
authTypeNames.Add("Application");
authTypeNames.Add("Bearer");
authTypeNames.Add(DefaultAuthenticationTypes.ExternalCookie);

Request.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());

我查看了其他问题,例如: OWIN authentication, expire current token and remove cookieOWIN - Authentication.SignOut() doesn't remove cookies

没有运气。我知道我可以通过设置负的到期日期来手动删除 cookie,但如果可能的话,我更愿意使用内置方法。

如何在我退出时删除应用程序 Cookie?

【问题讨论】:

  • 好吧.. 这不是第一次被指出stackoverflow.com/questions/22571696/…
  • @ymz,这是一个不同的问题。我问的是使用从另一个应用程序调用的外部身份验证服务器注销。

标签: c# authentication cookies asp.net-identity owin


【解决方案1】:

如果您有任何母版页,请添加以下标签。可能会有所帮助。

<meta http-equiv="Cache-control" content="no-cache" />

【讨论】:

【解决方案2】:

来自另一个对我有用的 StackOverFlow 答案:OWIN - Authentication.SignOut() doesn't seem to remove the cookie

只使用其中一种:

Request.GetOwinContext().Authentication.SignOut();
Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.Current.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

https://dzone.com/articles/catching-systemwebowin-cookie

我认为第二个对你有用,但看起来你正在这样做。你可以自己测试吗?注释掉您的数组并确认它是否有效。

老实说,我对 OWIN 的了解还不够,无法了解 Passive Authentication 模式。

【讨论】:

    【解决方案3】:

    为了让 SignOut 方法标记身份验证票证 (cookie) 以从客户端移除,您传递给 SignOut 方法的 AuthenticationType 参数和 cookie 上的值必须完全匹配。如果您想从客户端删除多个身份验证票证,则必须匹配所有这些 AuthenticationTypes 并将它们作为 string[] 传递给 SignOut 方法。

    身份验证票证的 AuthenticationType 通常以主机 Web 容器的名称(例如“.AspNet.”之类的名称)为前缀,后跟您引导 OWIN CookieAuthentication 设置的任何内容。

    看起来您在Startup.Auth.cs 中将 AuthenticationType 字符串值设置为“应用程序”。尝试简单地调用:

    Request.GetOwinContext().Authentication.SignOut("Application");
    

    如果这对您不起作用,我会调试您的应用程序并查看您的应用程序允许的每种经过身份验证的用户的身份的特定 AuthenticationType,记下每个用户的 AuthenticationType 值并尝试将它们全部包含在内在您的 SignOut 调用中的字符串 [] 中。

    【讨论】:

      【解决方案4】:
       AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
       FormsAuthentication.SignOut();
       Session.Abandon();
      

      【讨论】:

      • 这可能是对已经存在的其他答案的改进,但您没有解释它。虽然这个代码块可能会回答这个问题,但最好能稍微解释一下为什么会这样。
      【解决方案5】:

      我为此工作了几天。这是最终对我有用的方法。我要做的第一件事是清除令牌缓存。接下来,我创建了一个 Auth Application Types 数组。我添加了这 4 个。如果您正在使用它们,您可以添加更多。据我所知,我只使用 Cookie 和 OpenIdConnect,但为了安全起见,我添加了 Bearer 和 Application。最后一步是清除所有剩余的 Cookie(如果有)和任何剩余的会话(如果有)。同样,我为此工作了几天。这太令人沮丧了。我目前正在使用这些软件包中的 4.0.1。

      Install-Package Microsoft.Owin.Security.OpenIdConnect
      Install-Package Microsoft.Owin.Security.Cookies
      Install-Package Microsoft.Owin.Host.SystemWeb
      
      public ActionResult SignOut()
              {
                  
                  if (Request.IsAuthenticated)
                  {
                      string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
      
                      if (!string.IsNullOrEmpty(userId))
                      {
                          // Get the user's token cache and clear it
                          SessionTokenCache tokenCache = new SessionTokenCache(userId, HttpContext);
      
                          string sessionID = HttpContext.Session.SessionID;
      
                          tokenCache.Clear(sessionID);
                      }
                  }
      
                  var authTypeNames = new List<string>();
                  authTypeNames.Add("Cookies");
                  authTypeNames.Add("Application");
                  authTypeNames.Add("Bearer");
                  authTypeNames.Add("OpenIdConnect");
      
                  // Send a sign-out request. 
                  HttpContext.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());
      
                  Request.Cookies.Clear();
                  Session.RemoveAll();
      
                  return RedirectToAction("Index", "Home");
      
              }
      

      【讨论】:

        猜你喜欢
        • 2015-12-12
        • 2016-06-03
        • 1970-01-01
        • 2015-05-13
        • 2020-09-29
        • 1970-01-01
        • 2012-05-23
        • 2013-05-28
        相关资源
        最近更新 更多