Implementation of Remember Me On This Computer functioanility


Remember Me On This Computer functionality implementation in asp.net using cookies

Remember me on this computer


Sometimes we need to implement Remember me on this computer functionality. This can be done easily taking into reference the following.

TestPageRememberMe.aspx

Email Address: <asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox>

Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

<asp:CheckBox ID="chkRememberMe" runat="server" />Remember me on this computer

<asp:ImageButton ID="imgBtnSignIn" runat="server" ImageUrl="Image URL" OnClick="imgBtnSignIn_Click" AlternateText="sign in" />


TestPageRememberMe.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["MyWebsiteVisit"] != null)
{
txtEmail.Text = Request.Cookies["MyWebsiteVisit"]["Email"];
txtPassword.Attributes.Add("Value", Request.Cookies["MyWebsiteVisit"]["Password"]);
chkRememberMe.Checked = Convert.ToBoolean(Request.Cookies["MyWebsiteVisit"]["RememberMe"]);
}
}
}

protected void imgBtnSignIn_Click(object sender, ImageClickEventArgs e)
{
string _email = txtEmail.Text.Trim();
string _password = txtPassword.Text.Trim();
if (chkRememberMe.Checked == true)
{
Response.Cookies["MyWebsiteVisit"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["MyWebsiteVisit"]["Email"] = _email;
Response.Cookies["MyWebsiteVisit"]["Password"] = _password;
Response.Cookies["MyWebsiteVisit"]["RememberMe"] = true.ToString();
}
else
{
if (Request.Cookies["MyWebsiteVisit"] != null)
{
Response.Cookies["MyWebsiteVisit"].Expires = DateTime.Now.AddDays(-1);//set the expiry date 1 day ago
}
}
}


Comments

Guest Author: Ankur29 May 2012

txtPassword.Attributes.Add("Value", Request.Cookies["MyWebsiteVisit"]["Password"]);
There is some error in this line my pc says it has some assembly undefined
under the Word "Attributes" it shows a red Line.
plz help me soon



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: