Remember Me control in asp.net
Remember Me control in asp.net
In this article i did work on remember me control of asp.net.its not
an in built control. For doing this I used cookies.
Cookies is state management technique
in asp.net.it hold the data at client side and save it on user browser as a
text file.so when user come back on same page next time, initially its login credentials
check against its cookies .if it found a valid credentials from cookie file
then it redirect to another page, otherwise it put you on login page for login.
.aspx part
On deign page I just put two textbox,
a button control, a checkbox and a label.
<div>
Uname
:
<asp:TextBox ID="txtUName"
runat="server"></asp:TextBox>
Password :
<asp:TextBox ID="txtPWD" runat="server"></asp:TextBox>
<br />
<br />
<asp:CheckBox ID="chkRememberPassword"
runat="server"
Text="Remeneber
Me" />
<br />
<asp:Button ID="btnsubmit"
runat="server"
Text="submit"
OnClick="btnsubmit_Click"
/>
</div>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
.Cs Part
//Global Variables
string u_id;
string Uid = "kandy";
string pass = "Password";
Boolean chk_login = false;
protected void
Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
if (!IsPostBack)
{
if (Request.Cookies["UName"]
!= null)
u_id = Request.Cookies["UName"].Value;
if (Request.Cookies["PWD"]
!= null)
txtPWD.Text = Request.Cookies["PWD"].Value;
if (Request.Cookies["UName"]
!= null && Request.Cookies["PWD"] != null)
chkRememberPassword.Checked = true;
if (Request.Cookies["active"]
!= null)
{
if (Request.Cookies["active"].Value == "ok")
{
Response.Redirect("http://www.google.com/");
}
else
{
Label1.Text = "please sign in";
}
}
}
}
protected void
btnsubmit_Click(object sender, EventArgs e)
{
if (txtUName.Text == Uid && txtPWD.Text ==
pass)
{
if (chkRememberPassword.Checked == true)
{
Response.Cookies["UName"].Value
= txtUName.Text;
Response.Cookies["PWD"].Value
= txtPWD.Text;
Response.Cookies["UName"].Expires
= DateTime.Now.AddMonths(2);
Response.Cookies["PWD"].Expires
= DateTime.Now.AddMonths(2);
Response.Cookies["active"].Value
= "ok";
Session["kandy"] = "kandy";
Response.Redirect("http://www.yahoo.com/");
}
else
{
Response.Cookies["UName"].Expires
= DateTime.Now.AddMonths(-1);
Response.Cookies["PWD"].Expires
= DateTime.Now.AddMonths(-1);
Response.Redirect("http://www.yahoo.com/");
}
}
else
{
Label1.Text = "Please Re-Enter User Id
or password";
}
}
Comments
Post a Comment