gpt4 book ai didi

c# - Asp.net 身份注销不工作

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:52 25 4
gpt4 key购买 nike

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

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

在此之后, session 仍然没有被清除。有什么想法吗?

身份验证启动:

  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 上下文中获取它吗? p>

在您的代码末尾尝试以下操作。

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

关于c# - Asp.net 身份注销不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36558810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com