Cookie应用
在http协议中,Cookie是一个文本文件,它是服务器或脚本,用户维护用户信息的一种方式。
Cookie集合是Request对象和Response对象,经常用到的集合。
Cookie包含会话Cookie和永久Cookie两种。
会话Cookie——是临时性的,它只在浏览器打开时存在。
永久Cookie——则永久的保存在客户机上,并且,在指定过期日期之前均可用。
如果没有给Cookie设置过期日期,它将自动成为一个会话Cookie,如果设置,则会成为一个永久Cookie。
WebForm1.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>利用Cookie实现密码记忆</title>
<style type="text/css">
.style1
{
width: 200;
float: left;
height: 92px;
}
.style2
{
text-align: left;
}
.style3
{
text-align: left;
height: 33px;
}
.style4
{
height: 33px;
}
#form1
{
height: 114px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table align="left" cellpadding="0" cellspacing="0" class="style1"
style="font-size: small">
<tr>
<td class="style2">
用户名:</td>
<td>
<asp:TextBox ID="txtname" runat="server" Width="120px" AutoPostBack="True"
ontextchanged="txtname_TextChanged"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
密 码:</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password" Width="120px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style4">
<asp:CheckBox ID="ckbauto" runat="server" Text="记住密码" />
</td>
</tr>
<tr>
<td class="style2" colspan="2">
<asp:Button ID="Button1" runat="server" Text="登录" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="重置" />
</td>
</tr>
<tr>
<td class="style2" colspan="2"><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
<br />
</form>
</body>
</html>
WebForm1.aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//重置
protected void Button2_Click(object sender, EventArgs e)
{
txtname.Text = "";
txtpwd.Text = "";
Label1.Text = "";
}
//登录
protected void Button1_Click(object sender, EventArgs e)
{
//默认设置用户名、密码都为admin
if (txtname.Text.Trim().Equals("admin") && txtpwd.Text.Trim().Equals("admin"))
{
Session["username"] = txtname.Text.Trim();
//如果选择记住密码
if (ckbauto.Checked)
{
//判断admin是否已经存在
if (Request.Cookies["username"] == null)
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["userpwd"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["username"].Value = txtname.Text.Trim();
Response.Cookies["userpwd"].Value = txtpwd.Text.Trim();
}
}
Label1.Text = "登录成功";
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('用户名或密码错误!');", true);
}
}
//用户名改变时
protected void txtname_TextChanged(object sender, EventArgs e)
{
//当用户名不为空
if (Request.Cookies["username"] != null)
{
//判断输入的用户名,是否为admin
if (Request.Cookies["username"].Value.Equals(txtname.Text.Trim()))
{
//自动补全密码
txtpwd.Attributes["value"] = Request.Cookies["userpwd"].Value;
}
else
{
txtpwd.Attributes["value"] = "";
}
}
}
}
}
当用户第一次输入用户名和密码admin,选择记住密码,登录之后,第二次登录时,输入用户名如果用户名存在,密码就会自动显示。