This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18830|Reply: 0

[ASP.NET] asp.net Use cookies to remember passwords and log in automatically

[Copy link]
Posted on 4/8/2015 11:39:31 AM | | |
  1. string username = this.txtUserName.Text;//用户名
  2.         string password = this.txtPassword.Text;//密码
  3.         if (UserManager.LoginValidate(username, password) && this.ckState.Checked == true)//判断登录名和密码是否正确和是否选择了记住用户名和密码的复选框
  4.         {
  5.            //判断客户端浏览器是否存在该Cookie 存在就先清除
  6.             if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
  7.             {
  8.                 Response.Cookies["username"].Expires = System.DateTime.Now.AddSeconds(-1);//Expires过期时间
  9.                 Response.Cookies["password"].Expires = System.DateTime.Now.AddSeconds(-1);
  10.             }
  11.             else
  12.             {
  13.                  //向客户端浏览器加入Cookie (用户名和密码 最好是使用MD5加密)
  14.                 HttpCookie hcUserName1 = new HttpCookie("username");
  15.                 hcUserName1.Expires = System.DateTime.Now.AddDays(7);
  16.                 hcUserName1.Value = username;
  17.                 HttpCookie hcPassword1 = new HttpCookie("password");
  18.                 hcPassword1.Expires = System.DateTime.Now.AddDays(7);
  19.                 hcPassword1.Value = password;
  20.                 Response.Cookies.Add(hcUserName1);
  21.                 Response.Cookies.Add(hcPassword1);
  22.             }
  23. }
Copy code
------------------------ go to the page after logging in (it is best to write the master page with the motherboard page)------------


  1. if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
  2.                 {
  3.                     //用户曾登录
  4.                  
  5.                     username = Request.Cookies["username"].Value.ToString();  //读取Cookie
  6.                     password = Request.Cookies["password"].Value.ToString();//判断Cookie读取出来的用户名和密码是否能正确登录
  7.                     if (UserManager.LoginValidate(username, password))
  8.                     {
  9.                       //登录后的代码
  10.              }         }
Copy code


------------------------------- opt-out of cookies (plus an opt-out button)-------------------------------

Exit  


  1. HttpCookie hcUserName1 = new HttpCookie("username");   
  2. hcUserName1.Expires = System.DateTime.Now.AddDays(-7);   
  3. hcUserName1.Value = username;   
  4. HttpCookie hcPassword1 = new HttpCookie("password");   
  5. hcPassword1.Expires = System.DateTime.Now.AddDays(-7);   
  6. hcPassword1.Value = password;   
  7. Response.Cookies.Add(hcUserName1);   
  8. Response.Cookies.Add(hcPassword1);  
Copy code






Previous:The role of static in C#
Next:asp.net study the shopping cart item instructions you need to make
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com