ASP.NET 身份 2 与谷歌登录...注销不起作用



我希望能够注销当前登录的用户,尤其是在当前用户关闭浏览器的用例中,打开一个新的浏览器,前往登录页面。

这是我一直在尝试的...

private ActionResult DoLogout()/// check this out https://dzone.com/articles/catching-systemwebowin-cookie the sytem.web cookie monster
{
    var AuthenticationManager = HttpContext.GetOwinContext().Authentication;
    AuthenticationManager.SignOut();
    AuthenticationManager.SignOut( DefaultAuthenticationTypes.ApplicationCookie );
    Session.Abandon();
    var user = UserManager.FindByName( User.Identity.Name );
    if (user != null)
    {
        UserManager.UpdateSecurityStamp( user.Id ); // remove the old cookie so it can't be reused to re-log in - EWB
    }
     AuthenticationManager.SignOut();
    ClearCookies();
    Request.GetOwinContext().Authentication.SignOut( DefaultAuthenticationTypes.ApplicationCookie );// https://stackoverflow.com/questions/28999318/owin-authentication-signout-doesnt-seem-to-remove-the-cookie - stralos s answer
                                                                                                            // https://stackoverflow.com/questions/43675904/asp-net-identity-2-logging-out-other-sessions-using-security-stamp-after-pa
    AuthenticationManager.SignOut( DefaultAuthenticationTypes.ApplicationCookie );
    return Redirect("https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://"+ Url.Action( "Index", "Home", new { target = "_blank" } ) ); //https://stackoverflow.com/questions/27515518/asp-net-identity-external-login-wont-log-out - f belihocine answer
  }

但是,当我在此代码中登录时,被称为

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
    return RedirectToAction("LogOut");    // <--- here
}

因为用户处于破裂状态,因为我认为ASP.NET已登录,但是Google仍在登录....

任何帮助都将受到赞赏

您需要致电Google API才能完成此操作,请参阅此信息ASP.NET身份 - 外部登录 - 不会注销

这是技巧

的最终
  Request.GetOwinContext().Authentication.SignOut( DefaultAuthenticationTypes.ApplicationCookie );// https://stackoverflow.com/questions/28999318/owin-authentication-signout-doesnt-seem-to-remove-the-cookie - stralos s answer
  Request.GetOwinContext().Authentication.SignOut( DefaultAuthenticationTypes.ExternalCookie );                                                                                                // https://stackoverflow.com/questions/43675904/asp-net-identity-2-logging-out-other-sessions-using-security-stamp-after-pa

我注意到大多数地方都使用了应用程序cookie,但是在少数地方是外部的。.我将全部转换为app cookie,然后停止工作,然后我尝试了下一个。

这是启动图中的代码,您可以在其中看到两个cookie

app.UseCookieAuthentication(
            new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                SlidingExpiration = true,
                ExpireTimeSpan = TimeSpan.FromMinutes( 1 ),
                CookieName = "SP3GGS-ID2-cookie",
                //CookieSecure = CookieSecureOption.Always, // TODO: turn this on for prod/qa so only ssl is allowed - EWB - per https://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes( 1 ),
                        regenerateIdentity: ( manager, user ) => user.GenerateUserIdentityAsync( manager )
                        )
                }, 
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);// HERE EWB

最新更新