Cookie对象
\n
Cookie对象是HttpCookieCollection类的一个实例,它用于保存客户端浏览器请求的服务器页面,也可以用它存取非敏感性的用户信息,信息保存的时间可以根据需要设置。如果没有设置Cookie失效日期,那么它们仅保存到关闭浏览器程序为止;如果将Cookie对象的Expires属性设置为MinValue,则表示Cookie永远不会过期。Cookie存储的数据量很受限制,大多数浏览器支持的最大容量为4096字节,因此,一般不要用来保存数据集或其他大量数据。由于并非所有的浏览器都支持Cookie,并且数据住处是以明文文本的形式保存在客户端计算机中,因此最好不要保存敏感的、未加密的数据,否则会影响网络的安全性。
\n
新建一个网站,包括一个网页,代码如下:
\n
1、Default.aspx代码:
\n
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
\n
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
\n
<html xmlns=”http://www.w3.org/1999/xhtml” >
\n
<head runat=”server”>
\n
<title>无标题页</title>
\n
</head>
\n
<body>
\n
<form id=”form1″ runat=”server”>
\n
<div> 数据加密<br />
\n
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox><br />
\n
<asp:Button ID=”Button2″ runat=”server” Text=”数据加密” onClick=”Button2_Click” /><br />
\n
保存网站信息<br />
\n
<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>
\n
\n
<asp:Button ID=”Button3″ runat=”server” onClick=”Button3_Click” Text=”写” Height=”20px” Width=”44px” />
\n
<asp:Button ID=”Button4″ runat=”server” Text=”读” Height=”19px” onClick=”Button4_Click” Width=”58px” />
\n
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox> <br />
\n
<asp:Button ID=”Button1″ runat=”server” onClick=”Button1_Click” Text=”Button” /><br />
\n
<asp:Label ID=”Label1″ runat=”server” Height=”14px” Text=”Label” Width=”358px”></asp:Label><br />
\n
<asp:Label ID=”Label2″ runat=”server” Height=”13px” Text=”Label” Width=”358px”></asp:Label><br />
\n
<asp:Label ID=”Label3″ runat=”server” Height=”9px” Text=”Label” Width=”356px”></asp:Label><br />
\n
</div>
\n
</form>
\n
</body>
\n
</html>
\n
Default.aspx.cs代码:
\n
using System;
\n
using System.Data;
\n
using System.Configuration;
\n
using System.Web;
\n
using System.Web.Security;
\n
using System.Web.UI;
\n
using System.Web.UI.WebControls;
\n
using System.Web.UI.WebControls.WebParts;
\n
using System.Web.UI.HtmlControls;
\n
\n
public partial class _Default : System.Web.UI.Page
\n
{
\n
protected void Page_Load(object sender, EventArgs e)
\n
{
\n
\n
}
\n
protected void Button1_Click(object sender, EventArgs e)
\n
{
\n
if (Request.Cookies["userInfo"] != null)
\n
{
\n
this.Label1.Text = Request.Cookies["userInfo"]["userName"];
\n
this.Label2.Text = Request.Cookies["userInfo"]["lastVist"];
\n
}
\n
HttpCookie aCookie;
\n
for (int i = 0; i < Request.Cookies.Count; i++)
\n
{
\n
aCookie = Request.Cookies[i];
\n
this.Label3.Text = string.Format(“Cookie 名称={0}<br>Cookie 值={1}”, aCookie.Name, aCookie.Value);
\n
}
\n
}
\n
protected void Button2_Click(object sender, EventArgs e)
\n
{
\n
string strPwd = TextBox1.Text;
\n
Response.Cookies["strPwd"].Value = FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd, “md5″);
\n
Response.Write(Response.Cookies["strPwd"].Value.ToString());
\n
}
\n
protected void Button3_Click(object sender, EventArgs e)
\n
{
\n
HttpCookie makecookie = new HttpCookie(“Cookie”);
\n
makecookie.Value = this.TextBox2.Text;
\n
Response.Cookies.Add(makecookie);
\n
}
\n
protected void Button4_Click(object sender, EventArgs e)
\n
{
\n
HttpCookie readcookie = Request.Cookies["cookie"];
\n
this.TextBox3.Text = readcookie.Value;
\n
}
\n
}
\n