Asp.net身份登出无法正常工作

时间:2016-04-11 20:38:40

标签: c# asp.net-identity

我尽可能地尝试了,但我仍然无法注销当前用户。 目前我有以下代码:

_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;
    }

2 个答案:

答案 0 :(得分:0)

你需要在SignOut内拨打AuthenticationManager我看到你正在尝试上面但是你是从Owin context

获得的吗?

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

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

另一种方法是清除cookie(我已经再次看到你尝试过这个,但只用AuthCookie尝试一下),将年份设置为-1 ..看来当你Session.Abandon() cookie时仍然存在且与FormsAuthentication.SignOut()相同..在代码的末尾尝试这样的事情:

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

答案 1 :(得分:0)

您需要致电

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