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

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

一般通常情况下,都是使用cookie来记录用户名和密码。以下是部分代码:

第一登陆时候代码:

 
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, "用户名和密码不正确");
            }
        }

    } 

转载地址:http://qljab.baihongyu.com/

你可能感兴趣的文章
Security Considerations for AppLocker
查看>>
Oracle Forensics t00ls
查看>>
JetLeak Vulnerability: Remote Leakage Of Shared Buffers In Jetty Web Server [CVE-2015-2080]
查看>>
windows event logs分析
查看>>
python网络编程
查看>>
Linux Hardening & Security
查看>>
免费而强大的linux防火墙:APF
查看>>
DNS安全相关
查看>>
FakeIKEd
查看>>
LFI fuzzer
查看>>
Algorithmic Complexity Attacks and the Linux Networking Code
查看>>
今后工作必须要调查清楚的事情
查看>>
Subversion结合Apache使用LDAP认证
查看>>
RHCE 认证台湾制作
查看>>
RHCE Video Guide
查看>>
apache去掉用户权限
查看>>
BT 4安装到硬盘
查看>>
运用sslstrip进行中间人攻击(Bypass https)
查看>>
UNIX系统被删文件的恢复策略(ext3被删除的恢复)
查看>>
说话的注意事项
查看>>