Cookie對象
Cookie對象是HttpCookieCollection類的一個實例,它用于保存客戶端瀏覽器請求的服務器頁面,也可以用它存取非敏感性的用戶信息,信息保存的時間可以根據(jù)需要設置。如果沒有設置Cookie失效日期,那么它們僅保存到關閉瀏覽器程序為止;如果將Cookie對象的Expires屬性設置為MinValue,則表示Cookie永遠不會過期。Cookie存儲的數(shù)據(jù)量很受限制,大多數(shù)瀏覽器支持的最大容量為4096字節(jié),因此,一般不要用來保存數(shù)據(jù)集或其他大量數(shù)據(jù)。由于并非所有的瀏覽器都支持Cookie,并且數(shù)據(jù)住處是以明文文本的形式保存在客戶端計算機中,因此最好不要保存敏感的、未加密的數(shù)據(jù),否則會影響網(wǎng)絡的安全性。
新建一個網(wǎng)站,包括一個網(wǎng)頁,代碼如下:
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> 數(shù)據(jù)加密<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button2" runat="server" Text="數(shù)據(jù)加密" onClick="Button2_Click" /><br />
保存網(wǎng)站信息<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;
}
}
?石家莊網(wǎng)站建設的優(yōu)點:我們是專屬定制,制作符合您公司業(yè)務、風格的網(wǎng)站,這就做到給您制作的網(wǎng)站是網(wǎng)絡上獨一無二的網(wǎng)站,這樣更有利于搜索引擎的收錄。如果您喜歡網(wǎng)絡上某個風格的網(wǎng)站或者網(wǎng)站模板,我們是只仿制而不直接套用,我們會對其中的結構和樣式做增減優(yōu)化,做…
?競價排名營銷是一種通過 競價購買搜索引擎中關鍵詞或關鍵短語的廣告方式。競價排名營銷分為兩種類型。 第一種,直接為真正的搜索引擎結果的排名付費,也就是說付的錢越多,網(wǎng)頁所獲得的排名名也就越高。 第二種則更類似于普通的廣告。這種競價排名營銷方式通過…
?網(wǎng)站優(yōu)化對網(wǎng)站來說是非常重要的,直接決定和影響著網(wǎng)站所起到的作用,以及在各個地方當中的排名。在進行seo優(yōu)化的過程中,我們必須要堅持一定的原則,真正的把這些事情做得更好,才可以確保最終的結果,這對于我們來說是非常重要的事。 SEO優(yōu)化主要分為8小步: …