主页 > 知识库 > asp.net中使用cookie与md5加密实现记住密码功能的实现代码

asp.net中使用cookie与md5加密实现记住密码功能的实现代码

热门标签:团购网站 Linux服务器 阿里云 Mysql连接数设置 服务器配置 科大讯飞语音识别系统 电子围栏 银行业务

在做一个前台的登陆和后台的信息审核管理功能时,需要用到记住密码的模块:
虽然.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);
        }
    }

您可能感兴趣的文章:
  • ASP.NET中DES加密与解密MD5加密帮助类的实现代码
  • VB.NET实现的MD5加密算法示例【32位】
  • .NET MD5加密解密代码解析
  • asp.net实现的MD5加密和DES加解密算法类完整示例
  • asp.net实现md5加密
  • vb 中的MD5加密在asp.net中的实现
  • ASP.NET中MD5与SHA1加密的几种方法
  • 彻底解决ASP.NET MD5加密中文结果和ASP不一致的问题
  • asp.net下常用的加密算法MD5、SHA-1应用代码
  • asp.net中MD5 16位和32位加密函数
  • ASP.net中md5加密码的方法
  • .net core使用MD5加密解密字符串

标签:衡水 萍乡 大理 枣庄 江苏 衢州 广元 蚌埠

巨人网络通讯声明:本文标题《asp.net中使用cookie与md5加密实现记住密码功能的实现代码》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266