簡體   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