📅  最后修改于: 2020-12-27 13:37:27             🧑  作者: Mango
ASP.NET Cookie是一小段文本,用于存储用户特定的信息。每当用户访问站点时,Web应用程序都可以读取此信息。
当用户请求网页时,网络服务器不仅发送网页,还发送包含日期和时间的cookie。该cookie存储在用户硬盘上的文件夹中。
当用户再次请求该网页时,浏览器会在硬盘驱动器上查找与该网页关联的cookie。浏览器为用户访问的每个不同站点存储单独的cookie。
注意:Cookie限于小尺寸,只能用于存储4 KB(4096字节)的文本。
在ASP.NET应用程序中有两种存储cookie的方法。
我们可以将Cookie添加到Cookies集合中,也可以通过创建HttpCookie类的实例来添加。除了HttpCookie要求Cookie名称作为构造函数的一部分之外,两者的工作原理相同。
在以下示例中,我们将在HttpCookie类的帮助下创建并添加cookie。
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="CookieExample.aspx.cs" Inherits="CoockieExample.CookieExample" %>
using System;
using System.Web;
namespace WebFormsControlls
{
public partial class CookieExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//-------------- Creating Cookie --------------------------//
// Creating HttpCookie instance by specifying name "student"
HttpCookie cokie = new HttpCookie("student");
// Assigning value to the created cookie
cokie.Value = "Rahul Kumar";
// Adding Cookie to the response instance
Response.Cookies.Add(cokie);
//--------------- Fetching Cookie -------------------------//
var co_val = Response.Cookies["student"].Value;
Label1.Text = co_val;
}
}
}
在以下示例中,我们将cookie直接添加到Cookies集合中。
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="CoockieExample._Default" %>
using System;
using System.Web.UI;
namespace CoockieExample
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Setting expiring date and time of the cookies
Response.Cookies["computer"].Expires = DateTime.Now.AddDays(-1);
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = "";
// --------------- Adding Coockies ---------------------//
if (apple.Checked)
Response.Cookies["computer"]["apple"] = "apple";
if (dell.Checked)
Response.Cookies["computer"]["dell"] = "dell";
if (lenevo.Checked)
Response.Cookies["computer"]["lenevo"] = "lenevo";
if (acer.Checked)
Response.Cookies["computer"]["acer"] = "acer";
if (sony.Checked)
Response.Cookies["computer"]["sony"] = "sony";
if (wipro.Checked)
Response.Cookies["computer"]["wipro"] = "wipro";
// --------------- Fetching Cookies -----------------------//
if (Request.Cookies["computer"].Values.ToString() != null)
{
if (Request.Cookies["computer"]["apple"] != null)
Label2.Text += Request.Cookies["computer"]["apple"] + " ";
if (Request.Cookies["computer"]["dell"] != null)
Label2.Text += Request.Cookies["computer"]["dell"] + " ";
if (Request.Cookies["computer"]["lenevo"] != null)
Label2.Text += Request.Cookies["computer"]["lenevo"] + " ";
if (Request.Cookies["computer"]["acer"] != null)
Label2.Text += Request.Cookies["computer"]["acer"] + " ";
if (Request.Cookies["computer"]["sony"] != null)
Label2.Text += Request.Cookies["computer"]["sony"] + " ";
if (Request.Cookies["computer"]["wipro"] != null)
Label2.Text += Request.Cookies["computer"]["wipro"] + " ";
}else Label2.Text = "Please select your choice";
Response.Cookies["computer"].Expires = DateTime.Now.AddDays(-1);
}
}
}
输出:
本示例将选择的值存储为cookie。