ASP.NET中登录时记住用户名和密码(附源码下载)--ASP.NET

必需了解的:实例需要做的是Cookie对象的创建和对Cookie对象数据的读取,通过Response对象的Cookies属性创建Cookie,通过Request对象的Cookies可以读取Cookie对象的数据。

案例思路:创建并保存Cookie在TextBox里一定时间

login.aspx:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title>登录时记住用户名和密码</title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         用户名<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
14         密&nbsp;&nbsp;&nbsp;码<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br/>
15         <asp:CheckBox ID="CheckBox1" runat="server" />记住用户名和密码<br />
16         <asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click" />&nbsp;&nbsp;
17        <asp:Button ID="Button2" runat="server" Text="重置" OnClick="Button2_Click1"/>
18     </div>
19     </form>
20 </body>
21 </html>

login.aspx.cs:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class login : System.Web.UI.Page
 9 {
10     //页面加载时执行的内容 Start
11     protected void Page_Load(object sender, EventArgs e)
12     {
13         //第一次加载不满足条件,之后均满足(除Cookie到时间了),将Cookie数据输出到TextBox里
14         if(Request.Cookies["username"]!=null&&Request.Cookies["password"]!=null)
15         {
16             TextBox1.Text = Request.Cookies["username"].Value.ToString();
17             TextBox2.Text = Request.Cookies["password"].Value.ToString();
18         }
19     }
20     //页面加载时执行的内容 End
21     //重置按钮的功能的实现 Start
22     protected void Button2_Click1(object sender, EventArgs e)
23     {
24         this.FindButton(this);
25     }
26     private void FindButton(Control c)
27     {
28         if (c.Controls != null)
29         {
30             foreach (Control x in c.Controls)
31             {
32                 if (x is System.Web.UI.WebControls.TextBox)
33                 {
34                     ((System.Web.UI.WebControls.TextBox)x).Text = "";
35                 }
36                 FindButton(x);
37             }
38         }
39     }
40     //重置按钮的功能的实现 End
41     //登录按钮的功能的实现 Satrt
42     protected void Button1_Click(object sender, EventArgs e)
43     {
44         if (CheckBox1.Checked)
45         {
46             //创建Cookie对象,保存Cookie数据,设置Cookie保存时间
47             Response.Cookies["username"].Value = TextBox1.Text;
48             Response.Cookies["username"].Expires = DateTime.Now.AddSeconds(10);
49             Response.Cookies["password"].Value = TextBox2.Text;
50             Response.Cookies["password"].Expires = DateTime.Now.AddSeconds(10);
51             Response.Redirect("Test.aspx");
52         }
53     }
54     //登录按钮的功能的实现 End
55 }

 源码文件下载:ASP.NET中登录时记住用户名和密码.zip

文章系笔者原创,转载请注明出处,感谢合作!

原文地址:https://www.cnblogs.com/qikeyishu/p/7760896.html