014. asp.net实现记住密码的功能

本文介绍了一个使用ASP.NET实现的简单登录验证过程,其中包括了如何通过表单输入进行用户验证,并利用Session和Cookie来保持登录状态的方法。特别地,文章展示了如何在用户选择记住密码选项时设置Cookie的有效期。

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

 1  protected void Button1_Click(object sender, EventArgs e)
 2     {
 3         if (txtname.Text.Trim().Equals("mr") && txtpwd.Text.Trim().Equals("mrsoft"))
 4         {
 5             Session["username"] = txtname.Text.Trim();
 6             if (ckbauto.Checked)
 7             {
 8                 if (Request.Cookies["username"] == null)
 9                 {
10                     Response.Cookies["username"].Expires = DateTime.Now.AddDays(30);
11                     Response.Cookies["userpwd"].Expires = DateTime.Now.AddDays(30);
12                     Response.Cookies["username"].Value = txtname.Text.Trim();
13                     Response.Cookies["userpwd"].Value = txtpwd.Text.Trim();
14                 }
15             }
16             Response.Redirect("admin.aspx");
17         }
18         else
19         {
20             ClientScript.RegisterStartupScript(this.GetType(),"","alert('用户名或密码错误!');",true);
21         }
22     }
23     protected void txtname_TextChanged(object sender, EventArgs e)
24     {
25         if (Request.Cookies["username"] != null)
26         {
27             if (Request.Cookies["username"].Value.Equals(txtname.Text.Trim()))
28             {
29                 txtpwd.Attributes["value"] = Request.Cookies["userpwd"].Value;
30             }
31         }
32     }

 注: 对于txtname控件, 需要将其的AutoPostBack属性, 设置为True, 这样当用户输入用户名之后, 会自动回发到服务器, 执行TextChanged事件

转载于:https://www.cnblogs.com/wxylog/p/6110176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值