ASP.NET Membership "Remember Me" That Actually Works

For those of you who have come up against this problem in the past, I have discovered a pretty simple solution on how to make the "Remember Me" feature in the ASP.NET Membership Provider actually work.

For quite some time I have been simply dragging and dropping the "Login" control onto a page and "expecting" it to work.  Sure the logging in bit works just fine, but when I check the "Remember me next time" Checkbox, then close the browser window and return.... I am asked to log in again. WTF? The built in Login control has "Remember Me" built in and it doesn't remember?

So after some hunting and pecking, I decided to build my own login form. Turns out the Membership Provider does in fact have the ability to remember, it's just the Login control that does not.

Here's the basics of a login form.

<table border="0" cellpadding="0">  
                                    <tr> 
                                        <td>Username:</td> 
                                        <td> 
                                            <asp:TextBox ID="txtUsr" runat="server" Width="150px"></asp:TextBox> 
                                            <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="txtUsr" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1" ForeColor="#ffffff">*</asp:RequiredFieldValidator> 
                                        </td> 
                                    </tr> 
                                    <tr> 
                                        <td>Password:</td> 
                                        <td> 
                                            <asp:TextBox ID="txtPass" runat="server" TextMode="Password" Width="150px"></asp:TextBox> 
                                            <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="txtPass" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1" ForeColor="#ffffff">*</asp:RequiredFieldValidator> 
                                        </td> 
                                    </tr> 
                                    <tr> 
                                        <td colspan="2" style="padding-top: 5px;">  
                                            <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." /> 
                                        </td> 
                                    </tr> 
                                    <tr> 
                                        <td colspan="2"><span style="color: #ffcccc; font-weight: bold;">  
                                            <asp:Label ID="FailureText" runat="server" EnableViewState="False" Visible="false" /></span> &nbsp;</td> 
                                    </tr> 
                                    <tr> 
                                        <td align="right" colspan="2">  
                                            <asp:Button ID="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" /> 
                                        </td> 
                                    </tr> 
                                </table> 

You'll notice that I removed all references to the Login control and just built a basic form.

Then, believe it or not, the act of logging is is quite simple.

    Protected Sub Login_OnClick(ByVal sender As ObjectByVal e As System.EventArgs)  
        If Page.IsValid Then 
            If Membership.ValidateUser(txtUsr.Text, txtPass.Text) Then 
                FormsAuthentication.RedirectFromLoginPage(txtUsr.Text, RememberMe.Checked)  
            Else 
                FailureText.Visible = True 
            End If 
        End If 
    End Sub 

The bit that does all the work is the "FormsAuthentication.RedirectFromLoginPage".  The second parameter tells the system to set a cookie for 50 years (This can be changed programmatically, but I don't really care to get into all of that).



So there you have it.  A custom login page where the "Remember Me" checkbox actually works.




Until next time...
Chase

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: rockinthesixstring
Posted on: January 11, 2010 at 4:16 PM
Tags: , ,
Categories: DotNet, AspNet, General, Membership

Post Information: Permalink | Comments (1) | Post RSSRSS comment feed

DotNetShoutOut WebDevVote DotNetKicks ReddIt
shout it on DotNetShoutOut.com vote it on WebDevVote.com kick it on DotNetKicks.com submit to reddit

Comments

Add comment


(Will show your Gravatar icon)

 
Comment Mode
Preview Mode
 

Loading