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

时间:2017-12-04 21:37:30

标签: asp.net c#-4.0 asp.net-identity-2 google-login

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

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

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仍然登录....

感谢任何帮助

2 个答案:

答案 0 :(得分:1)

您需要调用Google API才能完成此操作,有关详细信息,请参阅此ASP.net identity - external login - won't log out

答案 1 :(得分:0)

这就是最终的伎俩

  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

我注意到大多数地方都使用了app cookie,但有一些是外部.. ..我将所有内容转换为app cookie并且它停止工作,然后我尝试了下一步。

以下是StartupAuth中的代码,您可以在其中看到正在使用两个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