程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net中使用cookie與md5加密實現記住密碼功能的實現代碼

asp.net中使用cookie與md5加密實現記住密碼功能的實現代碼

編輯:ASP.NET基礎

在做一個前台的登陸和後台的信息審核管理功能時,需要用到記住密碼的模塊:
雖然.net內置了登陸控件,有記住密碼的功能,但還是想自己實踐一下,以下代碼主要應用了COOKIE,包括安全加密的過程等。

復制代碼 代碼如下:
//設置,刪除Cookie
//provider jb51.net
        protected void set_cookie()
        {
            HttpCookie UserNameCookie = Request.Cookies["UserNameCookie"];
            HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
            if (this.CheSave.Checked)
            {
                lblcookie.Text = "1";
                //保存用戶名和密碼到cookie
                if (UserNameCookie == null)
                {
                    UserNameCookie = new HttpCookie("UserNameCookie");
                    UserNameCookie.Values.Add("UserName", TxtUserName.Text);
                    UserNameCookie.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(UserNameCookie);
                }
                //修改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");     //如果重新指定用戶密碼,則重新加密密碼
                    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”是程現在密碼框中的十個字符。
                {
                    SetToCookie(UserPasswordCookie, "UserPassword", FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5"));
                }
            }
            else
            {
                lblcookie.Text = "0";
                //從cookie刪除用戶名和密碼
                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);
                }
            }
        }
        //檢查是否存在COOKie情況
        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");//設置密碼框初始值
            }
        }
        public string getpassword()
        {
            HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
            string strpwd = "";  //獲取此密碼字符串與數據庫存中的密布相比。
            if (lblcookie.Text == "1")
            {
                strpwd = UserPasswordCookie.Values["UserPassWord"];//直接獲取COOKIE中的密碼值
            }
            else
            {
                strpwd= FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5"); //對密碼進行加密
            }
            return strpwd;  //返回密碼字段   
        } 
        //修改COOKIE這段的方法
        public void SetToCookie(HttpCookie httpcookie, string cookiename, string cookievalue)
        {
            httpcookie.Values[cookiename] = cookievalue;
            httpcookie.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(httpcookie);
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved