【问题标题】:"Remember me" with ASP.NET MVC Authentication is not working使用 ASP.NET MVC 身份验证的“记住我”不起作用
【发布时间】:2010-10-05 13:32:28
【问题描述】:

我有一个标准的 ASP.NET MVC(RC 刷新)Web 项目,其中包含标准的 ASP.NET 成员资格提供程序和项目模板中包含的帐户控制器。

当我在登录表单中选中“记住我”时,网站仍然没有记住我。 (Firefox 会记住我的用户名和密码,但我希望会自动登录)。

我必须手动设置和检查 cookie 吗?如果是,应该怎么做最好?

【问题讨论】:

    标签: .net asp.net-mvc cookies membership


    【解决方案1】:

    您需要将 true/false 传递给 SetAuthCookie 方法。

    public ActionResult Login (string email, string password, bool rememberMe, string returnUrl)  
    {
    
        // snip
    
        FormsAuth.SetAuthCookie(username, rememberMe); // <- true/false
    
        // snip
    }
    

    并确保bool rememberMe 反映了您登录页面上复选框的状态。

    【讨论】:

    • 我这样做了,立即生效,当我登录时,关闭浏览器,重新打开它并浏览到该站点,我立即再次登录 - 即使我没有尝试查看需要我的页面。这是它应该的方式,还是我让它太容易了?
    • SetAuthCookie 的 createPersistentCookie 导致将 cookie 保存在用户系统上,使他们保持登录状态。所以是的,这就是该参数的作用。还有另外两个控制登录的项目是“会话超时”和“授权超时”。
    • 如果会话超时但没有授权超时,当用户访问该站点时,他们是否需要在最初登录后重新登录并勾选记住我?
    • 还要确保在 web.config 中设置“超时”值。
    【解决方案2】:

    您需要在Controll方法中生成持久cookie,当检查记住ME框时处理登录时。如果您使用的是RedirectFromLoginPage,请将createPersistentCookie 参数设置为true

    【讨论】:

    • 我现在使用以下代码创建 cookie。 if (rememberMe) { HttpCookie cookie = FormsAuthentication.GetAuthCookie(userName, true); cookie.Expires = DateTime.Now.Add(new TimeSpan(30, 0, 0, 0)); Response.Cookies.Add(cookie);我如何检查请求中是否有有效的 cookie?
    • 我认为如果你得到一个有效的会话cookie,用户将被设置在HttpContext中并且他们不会被AuthorizationAttribute定向到你的登录页面。
    【解决方案3】:

    这 3 种方法帮助我持久化 cookie。

    注意,如果用户取消选择“记住我”,您将需要删除 cookie。

       private const string RememberMeCookieName = "MyCookieName";
    
    
    
            private string CheckForCookieUserName()
            {
                string returnValue = string.Empty;
                HttpCookie rememberMeUserNameCookie = Request.Cookies.Get(RememberMeCookieName);
                if (null != rememberMeUserNameCookie)
                {
                    /* Note, the browser only sends the name/value to the webserver, and not the expiration date */
                    returnValue = rememberMeUserNameCookie.Value;
                }
    
                return returnValue;
            }
    
            private void CreateRememberMeCookie(string userName)
            {
                HttpCookie rememberMeCookie = new HttpCookie(RememberMeCookieName, userName);
                rememberMeCookie.Expires = DateTime.MaxValue;
                Response.SetCookie(rememberMeCookie);
            }
    
            private void RemoveRememberMeCookie()
            {
                /* k1ll the cookie ! */
                HttpCookie rememberMeUserNameCookie = Request.Cookies[RememberMeCookieName];
                if (null != rememberMeUserNameCookie)
                {
                    Response.Cookies.Remove(RememberMeCookieName);
                    rememberMeUserNameCookie.Expires = DateTime.Now.AddYears(-1);
                    rememberMeUserNameCookie.Value = null;
                    Response.SetCookie(rememberMeUserNameCookie);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-11
      • 2011-06-18
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多