How to implement "Remember Me" in Login control
How to implement "Remember Me" in Login control?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Dec 23, 2009, 7:59 AM
Code the Remember me check box or Just Simply Use the Ready made Login Control .
I am here showing you the Coding method
if my answer helps you then check Do you like this answer" check box please :)
I have also Attached Example Project with this Post Please take a look ..
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie1 = Request.Cookies.Get("Username");
HttpCookie cookie2 = Request.Cookies.Get("Password");
HttpCookie cookie3 = Request.Cookies.Get("RememberMe");
if (cookie1 != null || cookie2 != null || cookie3 != null)
{
// Just Load the Values in TextBoxes From Cookies
txtUsername.Text = cookie1.Value.ToString();
txtPassword.Text = cookie2.Value.ToString();
CheckBox1.Checked = true;
}
else
{
// if Cookies Not Exist then Create it
HttpCookie cookie = new HttpCookie("RememberMe", "1");
HttpCookie usernamecookie = new HttpCookie("Username", txtUsername.Text);
HttpCookie passwordcookie = new HttpCookie("Password", txtPassword.Text);
usernamecookie.Expires = DateTime.Now.AddHours(1);
passwordcookie.Expires = DateTime.Now.AddHours(1);
cookie.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(cookie);
Response.Cookies.Add(usernamecookie);
Response.Cookies.Add(passwordcookie);
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
Response.Cookies["Username"].Value = txtUsername.Text;
Response.Cookies["Password"].Value = txtPassword.Text;
Response.Cookies["RememberMe"].Value = "1";
}
else if (CheckBox1.Checked == false )
{
Response.Cookies.Remove("RememberMe");
Response.Cookies.Remove("Username");
Response.Cookies.Remove("Password");
}
}
Hiren SoniPosted Dec 23, 2009, 7:21 AM
Lalit MPosted Dec 23, 2009, 7:19 AM
Different forums are filled with the questions regarding how to manually implement cookies for login or in other words how to implement "Remeber me" option.
Following is the code that will give the idea of how to achieve this task.
Controls used
1. TextBox, ID = TbUserName
2. TextBox, ID = TbPassword
3. CheckBox, ID = CbRememberMe
4. Button, ID = BtLogin
5. LinkButton, ID = lbSignout
------------------If you are using VB.Net-------------------------
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'Check if the browser support cookies
If Request.Browser.Cookies Then
'Check if the cookies with name PBLOGIN exist on user's machine
If Request.Cookies("PBLOGIN") IsNot Nothing Then
'Pass the user name and password to the VerifyLogin method
Me.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString())
End If
End If
End If
End Sub
Protected Sub BtLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'check if remember me checkbox is checked on login
If (Me.CbRememberMe.Checked) Then
'Check if the browser support cookies
If (Request.Browser.Cookies) Then
'Check if the cookie with name PBLOGIN exist on user's machine
If (Request.Cookies("PBLOGIN") Is Nothing) Then
'Create a cookie with expiry of 30 days
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30)
'Write username to the cookie
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
'Write password to the cookie
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
'If the cookie already exist then wirte the user name and password on the cookie
Else
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
End If
End If
End If
Me.VerifyLogin(Me.TbUserName.Text, Me.TbPassword.Text)
End Sub
Protected Sub VerifyLogin(ByVal UserName As String, ByVal Password As String)
Try
'If login credentials are correct
'Redirect to the user page
'else
'prompt user for invalid password
'end if
Catch ex as System.Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub lbSignout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbSignout.Click
'Check iIf the cookies with name PBLOGIN exist on user's machine
If (Request.Cookies("PBLOGIN") IsNot Nothing) Then
'Expire the cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30)
End If
'Redirect to the login page
End Sub
End Class
------------------If you are using C#.Net-------------------------
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//Check if the browser support cookies
if (Request.Browser.Cookies)
{
//Check if the cookies with name PBLOGIN exist on user's machine
if (Request.Cookies("PBLOGIN") != null)
{
//Pass the user name and password to the VerifyLogin method
this.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString());
}
}
}
}
protected void BtLogin_Click(object sender, System.EventArgs e)
{
//check if remember me checkbox is checked on login
if ((this.CbRememberMe.Checked))
{
//Check if the browser support cookies
if ((Request.Browser.Cookies))
{
//Check if the cookie with name PBLOGIN exist on user's machine
if ((Request.Cookies("PBLOGIN") == null))
{
//Create a cookie with expiry of 30 days
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30);
//Write username to the cookie
Response.Cookies("PBLOGIN").Item("UNAME") = this.TbUserName.Text;
//Write password to the cookie
Response.Cookies("PBLOGIN").Item("UPASS") = this.TbPassword.Text;
}
//If the cookie already exist then wirte the user name and password on the cookie
else
{
Response.Cookies("PBLOGIN").Item("UNAME") = this.TbUserName.Text;
Response.Cookies("PBLOGIN").Item("UPASS") = this.TbPassword.Text;
}
}
}
this.VerifyLogin(this.TbUserName.Text, this.TbPassword.Text);
}
protected void VerifyLogin(string UserName, string Password)
{
try
{
//If login credentials are correct
//Redirect to the user page
//else
//prompt user for invalid password
//end if
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}
}
protected void lbSignout_Click(object sender, System.EventArgs e)
{
//Check iIf the cookies with name PBLOGIN exist on user's machine
if ((Request.Cookies("PBLOGIN") != null))
{
//Expire the cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30);
}
//Redirect to the login page
}
}
OR
protected void Button1_Click(object sender, EventArgs e)
{
string uname = TextBox1.Text;string pass = TextBox2.Text;
if (Membership.ValidateUser(uname, pass)){
if (Request.QueryString["ReturnUrl"] != null){
FormsAuthentication.RedirectFromLoginPage(uname, Checkbox1.Checked);
// here i just assume that ur Remember Me checkbox id is Checkbox1;
}
else
{
FormsAuthentication.SetAuthCookie(uname, Checkbox1.Checked);
Response.Redirect("homepage.aspx");
//speciyfiy here page name on which u want to redirect}
}
else
{
Label1.Text="Invalid Id Or Password";}
}
reference