简体   繁体   中英

Asp.net identity Logout not working

I have tried every way possible, but I am still not able to logout the current user. Currently I have the following code:

_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();

After this, the session is still not cleared. Any ideas?

Authentication startup:

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

SignIn Code:

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

You need to call the SignOut within the AuthenticationManager I see you are trying above but are you getting it from the Owin context

try the below at the end of your code.

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

Another way is to clear the cookie (i have seen again you tried this above but try it with just the AuthCookie) by setting the year by -1 .. it seems when you Session.Abandon() the cookie is still there and same with FormsAuthentication.SignOut() .. try something like this at the end of your code:

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

你需要打电话

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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