主營(yíng)業(yè)務(wù):網(wǎng)站制作 網(wǎng)站優(yōu)化 小程序制作 石家莊網(wǎng)站制作網(wǎng)站建設(shè)有限公司歡迎您!
石家莊閃云網(wǎng)絡(luò)科技有限公司
客服熱線15383239821
如何讓自己的網(wǎng)站更有質(zhì)量?

.NET內(nèi)置對(duì)象之Cookie對(duì)象

發(fā)布時(shí)間:2010/9/28 17:09:11

Cookie對(duì)象

Cookie對(duì)象是HttpCookieCollection類的一個(gè)實(shí)例,它用于保存客戶端瀏覽器請(qǐng)求的服務(wù)器頁(yè)面,也可以用它存取非敏感性的用戶信息,信息保存的時(shí)間可以根據(jù)需要設(shè)置。如果沒有設(shè)置Cookie失效日期,那么它們僅保存到關(guān)閉瀏覽器程序?yàn)橹梗蝗绻麑ookie對(duì)象的Expires屬性設(shè)置為MinValue,則表示Cookie永遠(yuǎn)不會(huì)過(guò)期。Cookie存儲(chǔ)的數(shù)據(jù)量很受限制,大多數(shù)瀏覽器支持的最大容量為4096字節(jié),因此,一般不要用來(lái)保存數(shù)據(jù)集或其他大量數(shù)據(jù)。由于并非所有的瀏覽器都支持Cookie,并且數(shù)據(jù)住處是以明文文本的形式保存在客戶端計(jì)算機(jī)中,因此最好不要保存敏感的、未加密的數(shù)據(jù),否則會(huì)影響網(wǎng)絡(luò)的安全性。

新建一個(gè)網(wǎng)站,包括一個(gè)網(wǎng)頁(yè),代碼如下:

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>無(wú)標(biāo)題頁(yè)</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>

&nbsp; &nbsp; &nbsp;&nbsp;

<asp:Button ID="Button3" runat="server" onClick="Button3_Click" Text="寫" Height="20px" Width="44px" />

&nbsp; &nbsp;<asp:Button ID="Button4" runat="server" Text="讀" Height="19px" onClick="Button4_Click" Width="58px" />

&nbsp; &nbsp;<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;

}

}

相關(guān)新聞推薦

在線客服 : 服務(wù)熱線:15383239821 電子郵箱: 27535611@qq.com

公司地址:石家莊市新華區(qū)九中街江西大廈4062

備案號(hào):冀ICP備2022000585號(hào)-4