Cookie對象
Cookie對象是HttpCookieCollection類的一個實例,它用於保存客戶端瀏覽器請求的服務器頁面,也可以用它存取非敏感性的用戶信息,信息保存的時間可以根據需要設置。如果沒有設置Cookie失效日期,那麼它們僅保存到關閉瀏覽器程序爲止;如果將Cookie對象的Expires屬性設置爲MinValue,則表示Cookie永遠不會過期。Cookie存儲的數據量很受限制,大多數瀏覽器支持的最大容量爲4096字節,因此,一般不要用來保存數據集或其他大量數據。由於並非所有的瀏覽器都支持Cookie,並且數據住處是以明文文本的形式保存在客戶端計算機中,因此最好不要保存敏感的、未加密的數據,否則會影響網絡的安全性。
新建一個網站,包括一個網頁,代碼如下:
1、Default.aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div> 數據加密<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button2" runat="server" Text="數據加密" OnClick="Button2_Click" /><br />
保存網站信息<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="寫" Height="20px" Width="44px" />
<asp:Button ID="Button4" runat="server" Text="讀" Height="19px" OnClick="Button4_Click" Width="58px" />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
<asp:Label ID="Label1" runat="server" Height="14px" Text="Label" Width="358px"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Height="13px" Text="Label" Width="358px"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Height="9px" Text="Label" Width="356px"></asp:Label><br />
</div>
</form>
</body>
</html>
Default.aspx.cs代碼:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["userInfo"] != null)
{
this.Label1.Text = Request.Cookies["userInfo"]["userName"];
this.Label2.Text = Request.Cookies["userInfo"]["lastVist"];
}
HttpCookie aCookie;
for (int i = 0; i < Request.Cookies.Count; i++)
{
aCookie = Request.Cookies[i];
this.Label3.Text = string.Format("Cookie 名稱={0}<br>Cookie 值={1}", aCookie.Name, aCookie.Value);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string strPwd = TextBox1.Text;
Response.Cookies["strPwd"].Value = FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd, "md5");
Response.Write(Response.Cookies["strPwd"].Value.ToString());
}
protected void Button3_Click(object sender, EventArgs e)
{
HttpCookie makecookie = new HttpCookie("Cookie");
makecookie.Value = this.TextBox2.Text;
Response.Cookies.Add(makecookie);
}
protected void Button4_Click(object sender, EventArgs e)
{
HttpCookie readcookie = Request.Cookies["cookie"];
this.TextBox3.Text = readcookie.Value;
}
}