Thursday, April 12, 2012

How to create or implement Remember me Login Page asp.net and c#


 Login page source code in asp.net and c#  
 Hi,as you have seen various example above,please see one other example for login page using  
 Here i have implement Remember me on Login Page using checkbox. now i am going to explain  
 how to use Remember Me checkbox and login page code using asp.net and c# net.When you checked Remember me  
 checkbox it will maintain username and password for website user using Cookies.  
 When user click login button that will stores the username and password in the cookie.  
 first determine if the remember me checkbox is checked or not, if it is checked then store the username and   
 password in the cookie for the 1 month and if not checked then set the expiration date to 1 day in else condition  
 to destroy the cookie.  
 protected void Page_Load(object sender, EventArgs e)  
 {  
      if (!Page.IsPostBack)  
      {  
           if (Request.Cookies["UserName"] != null)  
                txtUserName.Text = Request.Cookies["UserName"].Value;  
           if (Request.Cookies["Password"] != null)  
                txtPassword.Text = Request.Cookies["Password"].Value;  
           if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)  
                chkRember.Checked = true;   
      }  
 }  
 protected void btnLogin_Click(object sender, EventArgs e)  
 {  
       string stringconnection = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;  
       MySqlConnection connection = new MySqlConnection(stringconnection);  
      try  
      {   
           int flag = 0;  
           if (chkRember.Checked == true)  
           {  
                Response.Cookies["UserName"].Value = txtUserName.Text;  
                Response.Cookies["Password"].Value = txtPassword.Text;  
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(1);  
                Response.Cookies["Password"].Expires = DateTime.Now.AddMonths(1);  
           }  
           else  
           {  
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(-1);  
                Response.Cookies["Password"].Expires = DateTime.Now.AddMonths(-1);  
           }   
           MySqlCommand com = new MySqlCommand("select * from tbl_user_details", connection);  
           connection.Open();  
           if (connection.State == ConnectionState.Open)  
           {  
                MySqlDataReader objSqlDataReader;  
                objSqlDataReader = com.ExecuteReader();  
                while (objSqlDataReader.Read())  
                {  
                     if (objSqlDataReader[2].ToString().Equals(txtUserName.Text) && objSqlDataReader[5].ToString().Equals(txtPassword.Text))  
                     {  
                          flag = 1;  
                          Session["UserName"] = txtUserName.Text;  
                          Session["UserType"] = objSqlDataReader[8].ToString();  
                          Response.Redirect("ContactUs.aspx", false);  
                     }  
                     else  
                     {  
                          ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");  
                     }  
                }  
           }  
      }  
      catch (Exception ex)  
      {  
           lblMsg.Text = ex.ToString();  
      }  
      finally   
      {  
           connection.Close();  
      }  
 }  
 Regard  
 Santosh Singh