C# Asp.net标识注销不工作
我已经尽了一切可能,但我仍然无法注销当前用户。 目前我有以下代码:C# Asp.net标识注销不工作,c#,asp.net-identity,C#,Asp.net Identity,我已经尽了一切可能,但我仍然无法注销当前用户。 目前我有以下代码: _authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); string sKey = (string)HttpContext.Current.Session["user"]; string sUser = Convert.ToString(HttpContext.Current.Cache[sKey
_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中的注销
,
我看到您正在尝试上述操作,但您是从Owin上下文中获得它的吗
请在代码末尾尝试以下操作
var authetication = HttpContext.Current.GetOwinContext().Authentication;
authentication.SignOut();
另一种方法是通过将年份设置为-1
,来清除cookie(我再次看到您在上面尝试了此操作,但仅使用AuthCookie进行了尝试)。。当您使用Session.discard()
时,cookie似乎仍然存在,并且与FormsAuthentication.SignOut()相同。。在代码末尾尝试以下操作:
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
authCookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(authCookie);
你需要打电话
HttpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.applicationcokie)代码>我正在使用autofac从Asp.net身份注册所有管理器,如下所示:builder.register(c=>HttpContext.Current.GetOwinContext().Authentication).As().InstancePerRequest();当我在命令中解决此问题时,我无法注销。当我通过控制器中的GetOwinContext直接请求AuthenticationManager时,它可以工作。我如何使其在我的命令中工作?您的登录代码是什么样子的?身份验证和启动配置?感谢您的回复。请参阅我文章中的编辑。实际的注销看起来不错,但您正在谈论会话。标识不使用会话进行身份验证,仅使用cookie。您是否有其他使用会话进行身份验证的代码?不,我不会在其他任何地方使用它。需要注意的是,当我在控制器中直接触发signout方法时,一切正常。从我的命令触发注销方法时,该方法不起作用。可能重复