Thursday, 26 December 2013

Keep me logged in functionality in C# ASP.NET

Sometimes a user logs in to your website, comes back tomorrow and.... has to log in again. To avoid this problem i am providing option to keep you from having to enter your user name and password every time someone return. If someone check the 'Keep me logged in' box, a small cookie will create on your computer that will let us know who you are next time you login again.

Solution:-

First you have to create Cookie on login button click as follows :

protected void btnLogin_Click(object sender, System.EventArgs e)
{
    string Username = txtUsername.Text;
    string Password = txtPassword.Text;

//Create Cookie and Store the Login Detail in it, if check box is marked like this :-
if ((CheckBox1.Checked == true)) { HttpCookie createCookie = new HttpCookie("LoginDetail"); createCookie.Values("Username") = txtUsername.Text.Trim(); createCookie.Values("Password") = txtPassword.Text.Trim(); createCookie.Expires = System.DateTime.Now.AddDays(1); Response.Cookies.Add(createCookie); } Response.Redirect("Page Name");//Page Name on which you want to transfer your current page } Then you have check if cookie exists (is remember me marked), if yes fill the details like this on Page Load Event :- protected void Page_Load(object sender, System.EventArgs e) { if ((Response.Cookies("LoginDetail") != null)) { string uname = Response.Cookies("LoginDetail").Values("Username").ToString(); string pass = Response.Cookies("LoginDetail").Values("Password").ToString(); Response.Redirect("Page Name");//Page Name on which you want to transfer your current page } }


That’s it!!…..Happy Programming...

1 comment:

  1. http://techthoughtcircle.blogspot.in/2013/12/convert-current-date-into-words-format.html

    ReplyDelete