博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 如何实现记住密码功能
阅读量:4624 次
发布时间:2019-06-09

本文共 1540 字,大约阅读时间需要 5 分钟。

protected void Page_Load(object sender, EventArgs e)

        {
            HttpCookie cookies = Request.Cookies["platform"];
            //判断是否有cookie值,有的话就读取出来
            if (cookies != null && cookies.HasKeys)
            {
                tbxUserName.Text = cookies["Name"];
                //tbxPwd.Text= cookies["Pwd"];
                tbxPwd.Attributes.Add("value", cookies["Pwd"]);
                this.chkState.Checked = true;
            }
        }
        protected bool getUser()
        {
            string userName = tbxUserName.Text.Trim();//用户名
            string userPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(tbxPwd.Text.Trim(), "MD5"); //MD5密码
            StringBuilder strWhere=new StringBuilder();
            strWhere.AppendFormat("auserName='{0}' and auserPassword='{1}'", userName, userPwd);
            bl.A_TUser user = new bl.A_TUser();
            DataSet ds = new DataSet();
            ds=user.GetList(strWhere.ToString());
            if (ds.Tables[0].Rows.Count>0)
            {
                if (chkState.Checked)//记录cookie值
                {
                      HttpCookie cookie = new HttpCookie("platform");
                      cookie.Values.Add("Name", tbxUserName.Text.Trim());
                      cookie.Values.Add("Pwd", tbxPwd.Text.Trim());
                      cookie.Expires = System.DateTime.Now.AddDays(7.0);
                      HttpContext.Current.Response.Cookies.Add(cookie);
                }
                //else
                //{
                //    if (Response.Cookies["platform"] != null)
                //        Response.Cookies["platform"].Expires = DateTime.Now;
                //}
                Session["userId"] = ds.Tables[0].Rows[0][0].ToString();
                Session["userName"] = ds.Tables[0].Rows[0][1].ToString();
                return true;
            }
            return false;
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (getUser())
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                MessageBox.Show(this, "用户名和密码不正确");
            }
        }

转载于:https://www.cnblogs.com/songtzu/archive/2012/04/20/2458932.html

你可能感兴趣的文章
C语言学习笔记--字符串
查看>>
关于七牛进行图片添加文字水印操作小计
查看>>
DataSource数据库的使用
查看>>
Luogu4069 SDOI2016 游戏 树链剖分、李超线段树
查看>>
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>