• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在做一个前台的登陆和后台的信息审核管理功能时,需要用到记住密码的模块:
虽然.net内置了登陆控件,有记住密码的功能,但还是想自己实践一下,以下代码主要应用了COOKIE,包括安全加密的过程等。

复制代码 代码如下:

//设置,删除Cookie
//provider ogeek.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);
        }
    }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net中Session缓存与Cache缓存的区别分析发布时间:2022-02-05
下一篇:
关于C#中DateTime常用方法概述发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap