.NET内置对象之Cookie对象

本文介绍如何在ASP.NET中利用Cookie对象存储和读取非敏感性的用户信息,演示了通过按钮点击事件实现数据的加密存储及网站信息的保存与读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Cookie对象

Cookie对象是HttpCookieCollection类的一个实例,它用于保存客户端浏览器请求的服务器页面,也可以用它存取非敏感性的用户信息,信息保存的时间可以根据需要设置。如果没有设置Cookie失效日期,那么它们仅保存到关闭浏览器程序为止;如果将Cookie对象的Expires属性设置为MinValue,则表示Cookie永远不会过期。Cookie存储的数据量很受限制,大多数浏览器支持的最大容量为4096字节,因此,一般不要用来保存数据集或其他大量数据。由于并非所有的浏览器都支持Cookie,并且数据住处是以明文文本的形式保存在客户端计算机中,因此最好不要保存敏感的、未加密的数据,否则会影响网络的安全性。

新建一个网站,包括一个网页,代码如下:

1Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W 3C //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>

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

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值