asp.net USES the cookie and md5 encryption implementation to remember the password function of the implementation code

  • 2020-05-27 04:44:10
  • OfStack

When doing the login function of the front desk and the information audit management function of the background, it is necessary to remember the password module:
Although.net built-in login control, has the function of remembering password, but still want to practice 1, the following code mainly applies COOKIE, including the process of security encryption.


// Set, delete Cookie
//provider ofstack.com
        protected void set_cookie()
        {
            HttpCookie UserNameCookie = Request.Cookies["UserNameCookie"];
            HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
            if (this.CheSave.Checked)
            {
                lblcookie.Text = "1";
                // Save the username and password to cookie
                if (UserNameCookie == null)
                {
                    UserNameCookie = new HttpCookie("UserNameCookie");
                    UserNameCookie.Values.Add("UserName", TxtUserName.Text);
                    UserNameCookie.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(UserNameCookie);
                }
                // Modify the COOKIE
                else if (UserNameCookie.Values["UserName"] != TxtUserName.Text)
                {
                    SetToCookie(UserNameCookie, "UserName", TxtUserName.Text);
                }
                if (UserPasswordCookie == null)
                {
                    UserPasswordCookie = new HttpCookie("UserPasswordCookie");
                    string password1 = FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5");     // If you re - specify the user password, re-encrypt the password 
                    UserPasswordCookie.Values.Add("UserPassword", password1);
                    UserPasswordCookie.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(UserPasswordCookie);
                }
                else if (UserPasswordCookie.Values["UserPassword"] != FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5") && TxtUserPassword.Text != "1234567890")  
                // " 1234567890 "Is now in the password box 10 A character. 
                {
                    SetToCookie(UserPasswordCookie, "UserPassword", FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5"));
                }
            }
            else
            {
                lblcookie.Text = "0";
                // from cookie Delete the username and password 
                if (Response.Cookies["UserNameCookie"] != null)
                {
                    HttpCookie myCookie = new HttpCookie("UserNameCookie");
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie);
                }
                if (Response.Cookies["UserPasswordCookie"] != null)
                {
                    HttpCookie myCookie = new HttpCookie("UserPasswordCookie");
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie);
                }
            }
        }
        // Check for presence COOKie situation 
        public void check_cookie()
        {
            HttpCookie UserNameCookie = Request.Cookies["UserNameCookie"];
            HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
            if (UserNameCookie != null)
            {
                this.CheSave.Checked = true;
                TxtUserName.Text = UserNameCookie.Values["UserName"];
            }
            if (UserPasswordCookie != null)
            {
                TxtUserPassword.Attributes.Add("value", "1234567890");// Sets the initial value of the password box 
            }
        }
        public string getpassword()
        {
            HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
            string strpwd = "";  // Gets the comparison of this password string with the density in the data store. 
            if (lblcookie.Text == "1")
            {
                strpwd = UserPasswordCookie.Values["UserPassWord"];// Direct access to COOKIE Password value in 
            }
            else
            { 
                strpwd= FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5"); // Encrypt the password 
            }
            return strpwd;  // Return password field     
        }  
        // Modify the COOKIE This is the way it works 
        public void SetToCookie(HttpCookie httpcookie, string cookiename, string cookievalue)
        {
            httpcookie.Values[cookiename] = cookievalue;
            httpcookie.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(httpcookie);
        }
    }


Related articles: