cookie记住密码

本文介绍如何使用C#代码实现密码记忆功能,包括如何将用户名和密码存入HTTP Cookie,以及如何在页面加载时从Cookie中读取并显示密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cookie记住密码:

C#代码收藏代码
  1. ///<summary>
  2. ///记住密码
  3. ///</summary>
  4. privatevoidRememberPassword()
  5. {
  6. if(cbRememberPW.Checked)
  7. {
  8. //记住密码
  9. System.Web.HttpCookienewCookie=newHttpCookie("user");
  10. newCookie.Values["username"]=tbUserName.Value.Trim();
  11. stringstrPW=tbPassword.Value.Trim();//最好加密后的密码放到COOKIE内
  12. newCookie.Values["password"]=strPW;
  13. newCookie.Expires=DateTime.Now.AddDays(15);
  14. Response.AppendCookie(newCookie);
  15. }
  16. else
  17. {
  18. System.Web.HttpCookiecookie=Request.Cookies["user"];
  19. if(cookie!=null)
  20. {
  21. cookie.Expires=DateTime.Now.AddDays(-2);
  22. Response.Cookies.Set(cookie);
  23. }
  24. }
  25. }

读取cookie中的密码

C#代码收藏代码
  1. //在页面加载时调用此方法
  2. ///<summary>
  3. ///读取cookie中的密码
  4. ///</summary>
  5. privatevoidReadPassword()
  6. {
  7. if(Request.Cookies["user"]!=null)
  8. {
  9. stringstrPassword=Request.Cookies["user"]["password"].ToString();
  10. tbUserName.Value=Request.Cookies["user"]["username"].ToString();
  11. cbRememberPW.Checked=true;
  12. }
  13. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值