首页 文章

OWIN SignOut不会删除cookie

提问于
浏览
18

我在外部身份验证服务器中使用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?

4 回答

  • 1

    来自另一个对我有用的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知道被动身份验证模式 .

  • 1

    为了使SignOut方法标记要从客户端删除的身份验证票证(cookie),您传递给SignOut方法的AuthenticationType参数和cookie上的值必须完全匹配 . 如果要从客户端删除多个身份验证票证,则必须匹配所有这些AuthenticationTypes并将其作为字符串[]传递给SignOut方法 .

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

    看起来您在 Startup.Auth.cs 中将AuthenticationType字符串值设置为"Application" . 尝试简单地调用:

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

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

  • 1
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
     FormsAuthentication.SignOut();
     Session.Abandon();
    
  • -3

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

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

相关问题