views:

63

answers:

1

I am having problems with my login page. It pulls through the Username correctly from the cookie, however the CheckBox for Remember Me does not become checked when I view the page, even though the code for setting it on the Page_Load is being executed.

LoggedIn event for setting the cookies

    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 event for Login page

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

My Web.Config contains the following information as well as a MachineKey, Is this correct?

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

EDIT

I solved this problem by using:

lLogin.RememberMeSet = true;

I assumed this would be the same as finding the RememberMe CheckBox and setting the checked state but obviously it was not. Just thought I would share this if anyone else was having similar problems.

+1  A: 

In the forms element of your web.config, have you tried the cookieless="UseCookies" attribute? I see that you have it for sessionState, but I believe you need it for forms too.

Forgotten Semicolon