【问题标题】:Asp.net identity Logout not workingAsp.net 身份注销不起作用
【发布时间】:2018-01-29 18:54:26
【问题描述】:

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

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

【问题讨论】:

  • 您的登录代码是什么样的?和Authentication启动配置?
  • 感谢您的回复。请参阅我的帖子中的编辑。
  • 实际的SignOut 看起来不错,但您正在谈论会话。 Identity 不使用会话进行身份验证,仅使用 cookie。您还有其他使用会话进行身份验证的代码吗?
  • 不,我没有在其他任何地方使用它。值得注意的是,当我直接在控制器中触发注销方法时,一切正常。从我的命令触发注销方法时,它不起作用。
  • ASP.Net Identity Logout的可能重复

标签: c# asp.net-identity


【解决方案1】:

您需要在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);

【讨论】:

  • 我正在使用 autofac 从 Asp.net 身份注册所有管理器,如下所示:builder.Register(c => HttpContext.Current.GetOwinContext().Authentication) .As() .InstancePerRequest ();当我在命令中解决此问题时,我无法注销。当我直接通过控制器中的 GetOwinContext 请求身份验证管理器时,它可以工作。我怎样才能让它在我的命令中工作?
【解决方案2】:

你需要打电话

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2012-09-06
    • 2010-11-07
    相关资源
    最近更新 更多