繁体   English   中英

Asp.net身份注销不起作用

[英]Asp.net identity Logout not working

我已经尝试了所有可能的方法,但是仍然无法注销当前用户。 目前,我有以下代码:

_authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

        string sKey = (string)HttpContext.Current.Session["user"];
        string sUser = Convert.ToString(HttpContext.Current.Cache[sKey]);
        HttpContext.Current.Cache.Remove(sUser);
        HttpContext.Current.Session.Clear();
        HttpContext.Current.Response.Cookies.Clear();
        HttpContext.Current.Request.Cookies.Clear();
        HttpContext.Current.Session.Abandon();

此后,仍无法清除该会话。 有任何想法吗?

身份验证启动:

  app.UseCookieAuthentication(new CookieAuthenticationOptions
           {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });

登录代码:

    public override ApplicationUser Handle([NotNull]LoginCommand command)
    {
        var user = _userManager.Find(command.Login, command.Password);
        if (user == null)
        {
            throw new RentalApplicationValidationException("No valid login");
        }

        _authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        var identity = _userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
        _authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

        return user;
    }

您需要在AuthenticationManager调用SignOut我看到您在上面尝试过,但是您是从Owin context

请在代码末尾尝试以下内容。

var authetication = HttpContext.Current.GetOwinContext().Authentication;
authentication.SignOut();

另一种方法是通过将年份设置为-1来清除cookie(我再次见过您在上面尝试过此操作,但仅使用AuthCookie尝试了它)。似乎当您Session.Abandon() ,cookie仍然存在并且与FormsAuthentication.SignOut() ..在代码末尾尝试以下操作:

HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
authCookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(authCookie);

你需要打电话

HttpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM