Page view counter

Login Control. Remember Me Not working

Last post 10-09-2008 6:59 PM by n_m. 6 replies.

Sort Posts:

  • Login Control. Remember Me Not working

    09-25-2008, 6:26 PM
    • Loading...
    • n_m
    • Joined on 01-05-2004, 1:26 PM
    • Bay Area, CA
    • Posts 10
    • Points 32

    Hi,

     I created a login page using the login control. I am also using a cusomter membershipprovider, with a new validateuser function that i wrote. However the rememberme checkbox does not seem to work. Do i need to write code to handle if the rememberme checkbox was checked and redirect to the destinationpageurl? I was under the impression that this would work automatically. Any help would be appreciated. Thanks! 

    N

  • Re: Login Control. Remember Me Not working

    09-26-2008, 3:38 AM
    Answer
    • Loading...
    • yrajasekhar
    • Joined on 07-23-2008, 10:16 AM
    • Pune
    • Posts 246
    • Points 1,800

    Hi you have to store a persistent cookie on user machine if remember me is checked see the following code (if part is when remember me is checked) 

     if (cbRememberMe.Checked)
                    {
                        int cookietimeout = 360;
                        FormsAuthenticationTicket authTicket = new
                            FormsAuthenticationTicket(txtUserName.Text, true, cookietimeout);
                        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        cookie.Expires = authTicket.Expiration;
                        HttpContext.Current.Response.Cookies.Set(cookie);
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(txtUserName.Text, false);
                    }
     

     

    Please mark post as answer if it helped you
    My Blog
  • Re: Login Control. Remember Me Not working

    09-26-2008, 2:57 PM
    • Loading...
    • n_m
    • Joined on 01-05-2004, 1:26 PM
    • Bay Area, CA
    • Posts 10
    • Points 32

    Thanks.Ill try this.

     Another question. If the user closes the browser is the login cookie still be preserved? I would like to have it so users can still close the browser and still be able to be auto logged in within a certain amount of time.

     

    Thanks

     

     

  • Re: Login Control. Remember Me Not working

    09-26-2008, 7:58 PM
    Answer
    • Loading...
    • hans_v
    • Joined on 01-29-2007, 4:03 PM
    • Posts 1,007
    • Points 5,838

    n_m:

    I was under the impression that this would work automatically.

    Well, it does. You'll need to set the correct properties in web.config.

    Generate a machine key using this online tool and place it in your web.config (if you don't, users are logged out when the application recycles). And then set the properties as below:

    <forms loginUrl="Login.aspx" protection="All" timeout="60000" name=".ASPXAUTH" slidingExpiration="true" /> 

    Proctection should be All, timeout is the number of minutes the cookie expires, name is the name of the cookie. Be carefull, since there's a Potential Security Hole when using a persistent security cookie!

     

  • Re: Login Control. Remember Me Not working

    09-26-2008, 10:50 PM
    Answer
    • Loading...
    • anas
    • Joined on 09-21-2006, 4:31 AM
    • Ramallah, Palestine
    • Posts 6,115
    • Points 48,415
    • Moderator

    n_m:
     Another question. If the user closes the browser is the login cookie still be preserved? I would like to have it so users can still close the browser and still be able to be auto logged in within a certain amount of time.

    To allow asp.net remember your users , you need to make sure that the authentication cookie will be persisted , this can be done by setting the following Login control properties :

          <asp:Login ID="Login1" runat="server" RememberMeSet="true" DisplayRememberMe="false">
            </asp:Login>

    Regards,

    Anas Ghanem.


    Note:Please Don't hesitate to click "Alert Moderators" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

  • Re: Login Control. Remember Me Not working

    09-28-2008, 7:33 PM
    Answer
    • Loading...
    • yasserzaid
    • Joined on 09-22-2007, 9:10 PM
    • Egypt
    • Posts 2,531
    • Points 15,311

    Hi

    try the below code

    Use this code:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    CheckBox cek = (CheckBox)Login1.FindControl("RememberMe");
    if (Request.Cookies["username"] == null Request.Cookies["username"].Value.ToString().Trim() == "")
    {
    cek.Checked = false;
    }
    else
    {

    Login1.UserName = Request.Cookies["username"].Value.ToString();
    }
    }
    }


    protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
    {
    CheckBox cek = (CheckBox)Login1.FindControl("RememberMe");
    if (cek.Checked == true)
    {
    HttpCookie cookie = new HttpCookie("username");
    cookie.Value = Login1.UserName;

    cookie.Expires = DateTime.Now.AddDays(1);//cookie Expires
    HttpContext.Current.Response.AppendCookie(cookie);
    }
    else {
    HttpContext.Current.Response.Cookies.Remove("username");
    }
    cek.Checked = false;
    }

    //--- another way

    protected void Page_Load(object sender, EventArgs e)

    {

    if (Request.Cookies["MyCookie"] != null)

    {

    TextBox pass = (TextBox)Login1.FindControl("Password"); pass.Attributes.Add("value", Request.Cookies["MyCookie"]["password"]); Login1.UserName = Request.Cookies["MyCookie"]["username"];

    }

    }

    protected void Login1_LoggedIn(object sender, EventArgs e)

    {

    HttpCookie cookie1 = new HttpCookie("MyCookie");

    cookie1.Values.Add("username", Login1.UserName);

    cookie1.Values.Add("password", Login1.Password);

    cookie1.Expires = DateTime.Now.AddDays(1);//cookie Expires HttpContext.Current.Response.AppendCookie(cookie1);

    }

     
    Good Luck
  • Re: Login Control. Remember Me Not working

    10-09-2008, 6:59 PM
    • Loading...
    • n_m
    • Joined on 01-05-2004, 1:26 PM
    • Bay Area, CA
    • Posts 10
    • Points 32

    Thanks everyone. I just wanted to followup. After some more research, i found the solution. I noticed that the cookie's were being created, but for some reason the login page would just not redirect. Then i found the following example on some forum, i added to the page_load and now the rememberme checkbox works.

    If User.Identity.IsAuthenticated = True Then
                Response.Redirect(Login1.DestinationPageUrl)
            End If
     

     

Page 1 of 1 (7 items)