ASP.NET身份验证“记住我”CheckBox没有保持检查状态

时间:2010-09-07 15:22:55

标签: asp.net forms-authentication

我的登录页面出现问题。它正确地从cookie中提取用户名,但是当我查看页面时,CheckBox for Remember Me不会被检查,即使正在执行在Page_Load上设置它的代码。

用于设置Cookie的LoggedIn事件

    protected void lLogin_LoggedIn(object sender, EventArgs e)
    {
        // If Remember me then set an appropriate cookie
        if (lLogin.RememberMeSet)
        {
            HttpCookie loginCookie = new HttpCookie("loginCookie");
            Response.Cookies.Remove("loginCookie");
            Response.Cookies.Add(loginCookie);
            loginCookie.Values.Add("username", lLogin.UserName.ToString());
            DateTime dtExpiry = DateTime.Now.AddDays(15);
            Response.Cookies["loginCookie"].Expires = dtExpiry;
        }

        // Set a cookie to expire after 1 second
        else
        {
            HttpCookie loginCookie = new HttpCookie("loginCookie");
            Response.Cookies.Remove("loginCookie");
            Response.Cookies.Add(loginCookie);
            loginCookie.Values.Add("username", lLogin.UserName.ToString());
            DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here
            Response.Cookies["loginCookie"].Expires = dtExpiry;
        }
    }

登录页面的Page_Load事件

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get username field to set focus
        TextBox txtUserName = (TextBox)lLogin.FindControl("UserName");

        if (!IsPostBack)
        {
            // For resetting the login url so that it doesn't have a return value in the URL
            if (Request.QueryString["ReturnURL"] != null)
            {
                Response.Redirect("~/Login.aspx", true);
            }


            if (Request.IsAuthenticated)
            {
                Response.Redirect("~/Main/Home.aspx", true);
            }

            // If login cookie exists pull username
            if (Request.Cookies["loginCookie"] != null)
            {
                HttpCookie loginCookie = Request.Cookies["loginCookie"];
                lLogin.UserName = loginCookie.Values["username"].ToString();
                CheckBox cb = (CheckBox)lLogin.FindControl("RememberMe");
                // This is being Executed which is why I am puzzled
                cb.Checked = true;
            }
        }

        this.SetFocus(txtUserName);         
    }

我的Web.Config包含以下信息以及MachineKey,这是正确的吗?

    <authentication mode="Forms">
  <forms loginUrl="Login.aspx" protection="All" timeout="60000" name="HRKCO" slidingExpiration="true" />
</authentication>
    <sessionState mode="InProc"  cookieless="UseCookies" timeout="30"/>

修改

我通过使用:

解决了这个问题
lLogin.RememberMeSet = true;

我认为这与查找RememberMe CheckBox并设置已检查状态相同,但显然不是。我想如果其他人遇到类似的问题,我会分享这个。

2 个答案:

答案 0 :(得分:1)

forms的{​​{1}}元素中,您是否尝试过web.config属性?我知道你有cookieless="UseCookies",但我相信你也需要sessionState

答案 1 :(得分:0)

我的问题的解决方案是我在Page_Load事件中设置复选框的方式。

解决方案

// If login cookie exists pull username
    if (Request.Cookies["loginCookie"] != null)
    {
        HttpCookie loginCookie = Request.Cookies["loginCookie"];
        lLogin.UserName = loginCookie.Values["username"].ToString();
        lLogin.RememberMeSet = true;
    }