Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Remember My Password At The Time Of Login in asp.net,please give me the code to remember the password by using check in the checkbox in asp.net
Posted

Have a look at this codeproject post Remember me next time in login control in asp.net[^]

Bear in mind - remembering a password negates the reason for having one in the first place
 
Share this answer
 
Put a CheckBox on your form to control whether or not the user wants to remember the password. If this is checked, store the username and password in a cookie. This is the cookie that you will be checking when the user comes back to your application later on. For bonus points, put an expiration date in the cookie so that the user will be required to log in again if they don't come back to your site for some period of time - 1 month seems a reasonable value.
 
Share this answer
 
Here you go ,

Step1:-
In aspx add these

XML
<tr>
                        <td width="12%" align="right"><input id="chkbxRempasss" type="checkbox" runat="server" name="checkbox" value="checkbox" /></td>
                        <td width="88%"><span class="table-col2_7">Remember my password </span></td>
                      </tr>


Step2:-
In Aspx.cs you can write something like this to store it in cookies

C#
public partial class Login : System.Web.UI.Page
    {
        string username = string.Empty;
        string pwd = string.Empty;
        string encytpwd = string.Empty;
        string UserID = string.Empty;
        ProcessService ProcessService = new ProcessService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["myCookie"] != null)       
            {            
                HttpCookie cookie = Request.Cookies.Get("myCookie");            
                //txtUserName.Value = cookie.Values["username"];            
                txtUserName.Attributes.Add("value", cookie.Values["username"]);
                //txtPwd.Value = cookie.Values["password"];            
                txtPwd.Attributes.Add("value", cookie.Values["password"]);

                Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);            
                //Response.Cookies[Txt_Password.Text].Expires = DateTime.Now.AddDays(-1);
                chkbxRempasss.Checked = true;
            }

            if (Page.Request.Params.Get("__EVENTTARGET") != null)
            {
                ChkUserLogin();
            }
        }
//Check Login Credentials
        protected void ChkUserLogin()
        {
            try
            {
                HttpCookie myCookie = new HttpCookie("myCookie"); 
                if (chkbxRempasss.Checked == true) 
                { 
                    myCookie.Values.Add("username", txtUserName.Text.ToString()); 
                    myCookie.Values.Add("password", txtPwd.Text.ToString()); 
                    myCookie.Expires = DateTime.Now.AddDays(15); 
                    Response.Cookies.Add(myCookie); 
                }

                username = txtUserName.Text.ToString();
                pwd = txtPwd.Text.ToString();
                encytpwd = Encrypt(pwd, "&%#@?,:*");
                string result = string.Empty;
                result = ProcessService.ChkUserLogin(username, encytpwd);
                if (result == "0")
                {
                    DataSet User = ProcessService.getLoginuserdetails(username);
                    UserID = User.Tables[0].Rows[0][0].ToString();
                    Session["UserID"] = UserID.ToString();
                    Response.Redirect("Default.aspx");
                }
                else if (result == "1")
                {
                    lblAlert.Text = "Invalid UserName.";
                    lblAlert.ForeColor = System.Drawing.Color.Red;
                    lblAlert.Visible = true;
                }
                else if (result == "2")
                {
                    lblAlert.Text = "Invalid Password.";
                    lblAlert.ForeColor = System.Drawing.Color.Red;
                    lblAlert.Visible = true;
                }
            }
            catch (Exception exn)
            {
            }
            finally
            {
            }
        }


All the best:) write back if you need further support!
 
Share this answer
 
Comments
Richard Deeming 16-Jul-19 13:50pm    
Don't know why this question has popped back up into the active list, but while it's here, a note for anyone who intends to use this answer:

DON'T DO THIS!
NEVER store the user's credentials in a cookie.

All modern browsers have built-in password managers, which puts the user in control, and keeps the password stored securely. Let the browser manage the "remember my password" functionality.
Richard MacCutchan 26-Aug-19 3:42am    
A bit late Richard.
Richard Deeming 27-Aug-19 7:08am    
Yes - as I said, I don't know why this popped back into the active list. I just wanted to flag how dangerous this answer is to anyone who stumbles across it. :)
Richard MacCutchan 27-Aug-19 7:21am    
Old questions often get back to the top of the queue if a spammer attempts to, or succeeds in, posting their message there.

But, I agree, your comment is still quite valid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900