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

本文共 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

你可能感兴趣的文章
BZOJ 1901 Zju2112 Dynamic Rankings 题解
查看>>
C++虚析构函数
查看>>
《玩转.NET Micro Framework 移植-基于STM32F10x处理器》--微软中国.NET Micro Framework项目组工程师所作之序...
查看>>
php服务端搜索,功能改进
查看>>
unity, 在surface shader中访问顶点色
查看>>
Spring声明式事务配置
查看>>
并查集的实现
查看>>
Leetcode 350. Intersection of Two Arrays II
查看>>
EditPlus VC2010 and 2008 C/C++配置
查看>>
Practical Lessons from Predicting Clicks on Ads at Facebook
查看>>
JFrame面板
查看>>
Android自动化压力测试之Monkey Test 异常解读(五)
查看>>
Compressing Convolutional Neural Networks in the Frequency Domain 论文笔记
查看>>
设计模式:单例和多例
查看>>
Myslq 之修改数据库
查看>>
maven工程转为web工程时没有add web project capabilities选项的解决办法
查看>>
[BZOJ1192][HNOI2006]鬼谷子的钱袋
查看>>
正则表达式之 数据验证 与 文本替换
查看>>
CLR via C#:CLR的执行模型
查看>>
JS获取服务器时间
查看>>