当前位置: 首页 > 编程日记 > 正文

ASP.NET Cookie

最经在使用Cookie的过程中遇到了一些疑问,查阅参考MSDN,记录Cookie相关知识点

什么是Cookie

Cookie是一小段文本信息,伴随着用于的请求和页面在Web服务器和浏览器之间传递,并将它存储在用户硬盘上的某个文件夹中。Cookie包含每次用户访问站点时Web应用程序都可以读取的信息。以后,如果用户在此请求特定站点中的页面,当用户输入特定URL时,浏览器会在本地硬盘上查找与该URL关联的Cookie。如果该Cookie存在,浏览器将该Cookie与页面请求一起发送到你的站点。Cookie与网站关联,而不是与特定页面关联。因此,无论用户请求站点中的哪一个页面,浏览器和服务器都交换Cookie信息。用户访问不同站点时,各个站点都可能会向用户浏览器发送一个Cookie,浏览器会分别存储所有Cookie。

Cookie限制

大多数浏览器支持最大为4096字节的Cookie,这限制了Cookie的大小,最好用Cookie来存放少量的数据。浏览器还限制站点可以在用户计算机上存储的Cookie的数量。大多数浏览器只允许每个站点存储20个Cookie。如果试图存储更多Cookie,则最旧的Cookie便会被丢弃。有些浏览器还会对他们将接受的来自所有站点的Cookie总数做出绝对限制,通常为300个。

Cookie类定义

    // Summary://     Provides a type-safe way to create and manipulate individual HTTP cookies.public sealed class HttpCookie{// Summary://     Creates and names a new cookie.//// Parameters://   name://     The name of the new cookie.public HttpCookie(string name);//// Summary://     Creates, names, and assigns a value to a new cookie.//// Parameters://   name://     The name of the new cookie.////   value://     The value of the new cookie.public HttpCookie(string name, string value);// Summary://     Gets or sets the domain to associate the cookie with.//// Returns://     The name of the domain to associate the cookie with. The default value is//     the current domain.public string Domain { get; set; }//// Summary://     Gets or sets the expiration date and time for the cookie.//// Returns://     The time of day (on the client) at which the cookie expires.public DateTime Expires { get; set; }//// Summary://     Gets a value indicating whether a cookie has subkeys.//// Returns://     true if the cookie has subkeys, otherwise, false. The default value is false.public bool HasKeys { get; }//// Summary://     Gets or sets a value that specifies whether a cookie is accessible by client-side//     script.//// Returns://     true if the cookie has the HttpOnly attribute and cannot be accessed through//     a client-side script; otherwise, false. The default is false.public bool HttpOnly { get; set; }//// Summary://     Gets or sets the name of a cookie.//// Returns://     The default value is a null reference (Nothing in Visual Basic) unless the//     constructor specifies otherwise.public string Name { get; set; }//// Summary://     Gets or sets the virtual path to transmit with the current cookie.//// Returns://     The virtual path to transmit with the cookie. The default is the path of//     the current request.public string Path { get; set; }//// Summary://     Gets or sets a value indicating whether to transmit the cookie using Secure//     Sockets Layer (SSL)--that is, over HTTPS only.//// Returns://     true to transmit the cookie over an SSL connection (HTTPS); otherwise, false.//     The default value is false.public bool Secure { get; set; }//// Summary://     Gets or sets an individual cookie value.//// Returns://     The value of the cookie. The default value is a null reference (Nothing in//     Visual Basic).public string Value { get; set; }//// Summary://     Gets a collection of key/value pairs that are contained within a single cookie//     object.//// Returns://     A collection of cookie values.public NameValueCollection Values { get; }// Summary://     Gets a shortcut to the System.Web.HttpCookie.Values property. This property//     is provided for compatibility with previous versions of Active Server Pages//     (ASP).//// Parameters://   key://     The key (index) of the cookie value.//// Returns://     The cookie value.public string this[string key] { get; set; }}

Cookie编程

浏览器负责管理用户系统上的Cookie,Cookie通过HttpResponse对象发送到浏览器

Response.Cookies["TestOne"].Value = "这是第一种添加Cookie的方式";HttpCookie cookie = new HttpCookie("TestTwo", "这是第二种添加Cookie的方式");
Response.Cookies.Add(cookie);

多值Cookie

可以在Cookie中存储一个值,也可以在一个Cookie中存储多个名称/值对。名称/值对称为子键

Response.Cookies["Student"]["FirstName"] = "";
Response.Cookies["Student"]["LastName"] = "";
Response.Cookies["Student"].Expires =DateTime.Now.AddDays(10);

控制Cookie的范围

默认情况下,一个站点的全部Cookie都将一起存储在客户端上,而且所有Cookie都会随着对该站点发送的任何请求一起发送到服务器。可以通过两种方式设置Cookie的范围:

  1. 将Cookie的范围限制到服务器上的某个文件夹,这允许你将Cookie限制到站点上的某个应用程序
  2. 将范围设置为某个域,这允许你指定域中的哪些子域可以访问Cookie

将Cookie限制到某个文件夹或应用程序

将Cookie限制到服务器上某个文件夹,需设置Cookie的Path属性

 Response.Cookies["Student"]["FirstName"] = "";Response.Cookies["Student"]["LastName"] = "";Response.Cookies["Student"].Expires = DateTime.Now.AddDays(10);Response.Cookies["Student"].Path = "/Students";

如果站点名称为www.School.com,则在前面创建的Cookie只能用于路径http://www.School.com/Students的页面及该文件夹下的所有页面。

限制Cookie的域范围

默认情况下,Cookie与特定域关联。如果站点具有子域,则可以将Cookie于特定的子域关联,若要执行此操作,请设置Cookie的Domain属性。

Response.Cookies["Student"]["FirstName"] = "";
Response.Cookies["Student"]["LastName"] = "";
Response.Cookies["Student"].Expires = DateTime.Now.AddDays(10);
Response.Cookies["Student"].Domain = "support.contoso.com";

读取Cookie

if (Request.Cookies["Student"] != null)
{
HttpCookie cookie
= Request.Cookies["Student"];string name = Server.HtmlEncode(cookie.Value);
}

读取子键:

if (Request.Cookies["Student"] != null)
{string firstName = Server.HtmlEncode(Request.Cookies["Student"]["FirstName"]);string lastName = Server.HtmlEncode(Request.Cookies["Student"]["LastName"]);
}

Cookie中的子键被类型化为NameValueCollection类型集合

if (Request.Cookies["Student"] != null)
{NameValueCollection collection = Request.Cookies["Student"].Values;
}

修改和删除Cookie

不能直接修改Cookie,更改Cookie的过程设计创建一个具有新值的新Cookie,然后将其发送到浏览器来覆盖客户端的旧版本Cookie,下面的代码示例演示如何更改存储用户对站点的访问次数的 Cookie 的值:

int counter;
if (Request.Cookies["counter"] == null)counter = 0;
else
{counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++;Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1);

删除Cookie

删除 Cookie(即从用户的硬盘中物理移除 Cookie)是修改 Cookie 的一种形式。由于 Cookie 在用户的计算机中,因此无法将其直接移除。但是,可以让浏览器来为您删除 Cookie。该技术是创建一个与要删除的 Cookie 同名的新 Cookie,并将该 Cookie 的到期日期设置为早于当前日期的某个日期。当浏览器检查 Cookie 的到期日期时,浏览器便会丢弃这个现已过期的 Cookie:

 Response.Cookies["Student"]["FirstName"] = "";Response.Cookies["Student"]["LastName"] = "";Response.Cookies["Student"].Expires = DateTime.Now.AddDays(-10);

删除子Cookie

若要删除单个子键,可以操作 Cookie 的 Values 集合,该集合用于保存子键。首先通过从 Cookies 对象中获取 Cookie 来重新创建 Cookie。然后您就可以调用 Values 集合的 Remove 方法,将要删除的子键的名称传递给 Remove 方法。接着,将 Cookie 添加到 Cookies 集合,这样 Cookie 便会以修改后的格式发送回浏览器。

转载于:https://www.cnblogs.com/PerfectSoft/archive/2012/05/31/2529319.html

相关文章:

1111 评论

201406114205 陈嘉慧 http://www.cnblogs.com/hui1005039632/ 201406114219 林宇粲 http://www.cnblogs.com/zlcan/ 201406114220 蔡舜 http://www.cnblogs.com/caishun/ 201406114215 林志杰 http://www.cnblogs.com/15linzhijie/ 201406114252 王俊杰 http://www.cnblogs.c…

React 16.8.6 发布,构建用户界面的 JavaScript 库

React 16.8.6 已发布,该版本更新如下: React DOM 修复 useReducer() 中的问题(acdlite in #15124)修复 Safari DevTools 中的 iframe 警告(renanvalentin in #15099)若 contextType 设置为 Context.Consume…

linux禁止路由器,FCC 新规可能禁止在 WiFi 路由器安装 OpenWRT

FCC(美国联邦通讯委员会)的新规则可能会禁止在 WiFi 路由器安装 OpenWRT。OpenWrt 类似于 Buildroot 的路由器固件,为嵌入式设备所研发的 Linux 发行版。目前 OpenWrt 已支持多个平台(如 ARM、mips、x86 等),且提供了许多开源应用程序!许多便…

智销功能_Shiro权限框架

Shiro是什么? Spring security 重量级安全框架 Apache shiro 轻量级安全框架 Shiro是一个强大且易用的Java权限框架 四大基石 身份验证,授权,密码学,会话管理 /*** String algorithmName, Object source, Object salt, int hashIt…

ARM、FPGA和DSP的特点和区别是什么?(转)

发布时间:2009-5-8 14:25 发布者:ARM 关键词:DSP, ARM, FPGA, 特点 DSP(digital singnal processor)是一种独特的微处理器,有自己的完整指令系统,是以数字信号来处理大量信息的器件。一个…

unix to linux,UNIX to Linux 的关键问题都有哪些?

答:针对问题描述有一些不同的观点。1、第一个问题就是应用架构的改造问题,需要支持负载均衡模式。说明:这个不一定需要支持负载均衡模式,首先本身LINUXONE提供多分区架构,不需要改变原有应用系统的部署模式。而且负载均…

MongoDb 查询时常用方法

Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name", "a"), Query.EQ("title", "t"));//同时满足多个条件Query.EQ("name", "a");//等于Query.Exist…

解决Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/

https://blog.csdn.net/quanqxj/article/details/79479943转载于:https://www.cnblogs.com/liuys635/p/10624068.html

从 StarCraft 2 Installer.exe 中提取种子文件

蛋疼的想在 Linux 下下载星际争霸,但是暴雪提供的是 exe 格式的文件,这其实就是个 BT 客户端,但是问题是怎么提取出里面的种子文件呢,经过一番 google 找到了答案。 直接用 Vi 或 Emacs 打开 exe 格式的文件,搜索“d8:…

linux下接口持续集成,部署jenkins持续集成工具

1、Linux安装配置jdk环境1.1、上传到 Linux 服务器;例如:上传至: cd /usr/local1.2、解压:rpm -ivh jdk-8u111-linux-x64.rpm1.3、环境变量配置cd /etc在etc下,找到 profile文件,增加如下如下配置&#xff…

iOS UILabel UITextView自适应文本,或文本大小自适应

//UILabel自适应文本的高度UILabel *label [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];label.numberOfLines 0;label.lineBreakMode NSLineBreakByWordWrapping;label.text "是它吗?哈哈,太兴奋了。”12日,随着土…

(原)War3 脚本分析5-基础脚本资源

众所周知War3编辑器非常强大,这种强大不仅是因为其拥有诸如地形编辑器、开关编辑器、声音编辑器、物体编辑器、战役编辑器、AI编辑器、物体管理器、输入管理器等非常全面且易于使用的功能,更为重要的是在其基础上MOD爱好者通过很简单的操作即可实现各式各…

Mysql统计分组区间的人数和

统计各分数区间数据 现在要统计&#xff1a;<50、50-60、60-70、70-80、80-90、90-100、>100分数区间的人数&#xff1b;利用 INTERVAL 划出7个区间&#xff1b;再利用 elt 函数将7个区间分别返回一个列名&#xff0c;如下SQL&#xff1a; 123456789101112131415 mysql&g…

tcl c语言笔试题,TCL技术类笔试题目.doc

TCL技术类笔试题目模拟电路试题一&#xff0e;二极管1.如图所示电路中&#xff0c;已知电源电压 E4V 时,I1mA。那么当电源电压 E8V 时 , 电流I的大小将是______2.稳压管通常工作于______&#xff0c;来稳定直流输出电压截止区 正向导通区 反向击穿区3. 由二极管的伏安特性可知&…

Js-函数式编程

前言 JavaScript是一门多范式语言&#xff0c;即可使用OOP&#xff08;面向对象&#xff09;&#xff0c;也可以使用FP&#xff08;函数式&#xff09;&#xff0c;由于笔者最近在学习React相关的技术栈&#xff0c;想进一步深入了解其思想&#xff0c;所以学习了一些FP相关的知…

MFC中显示 .bmp格式的位图

最近在看VisualC 图像处理的书籍&#xff0c;表示一直在从基础做起&#xff0c;今天就记录一个简单功能的实现&#xff0c;显示.bmp格式的位图。 首先需要理解的是窗口创建的过程包括两个步骤&#xff1a;首先擦除窗口的背景&#xff0c;然后在对窗口进行重新绘制。 一般而言&a…

ibatis源码浅析- 初探

ibatis核心类 SqlMapExecutor&#xff1a;定义了数据库curd操作api SqlMapTransactionManager &#xff1a; 主要定义了事务管理功能 SqlMapClient&#xff1a;继承SqlMapExecutor, SqlMapTransactionManager接口 也就具有curd操作 事务管理行为 SqlMapSession&#xff1a; 它…

c语言中void跟argv,argc和argv []在C语言中

我学习C和在其中一个例子&#xff0c;我们写出这样的程序&#xff1a;argc和argv []在C语言中#include int main(int argc, char *argv[]){// go through each string in argvint i 0;while(i < argc){printf("arg %d: %s\n", i, argv[i]);i;}// lets make our o…

【转】apache常用配置

如何设 置请求等待时间在httpd.conf里面设置&#xff1a;  TimeOut n  其中n为整数&#xff0c;单位是秒。 如何接收一个get请求的总时间接收一个post和put请求的TCP包之间的时间  TCP包传输中的响应&#xff08;ack&#xff09;时间间隔 如何使得apache监听在特定的端口…

[20190402]对比_mutex_wait_scheme不同模式cpu消耗.txt

[20190402]对比_mutex_wait_scheme不同模式cpu消耗.txt--//前几天做了sql语句在mutexes上的探究.今天对比不同_mutex_wait_scheme模式cpu消耗.1.环境:SYSbook> hide mutexNAME DESCRIPTION DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE---------------…

【宋红康学习日记11】Object类与equals方法

1 &#xff08;1&#xff09;当对象是基本数据类型时&#xff0c;比较值&#xff1b; &#xff08;2&#xff09;当对象是引用型时&#xff0c;比较的是地址值&#xff01;&#xff01;1 2 equals&#xff08;&#xff09;&#xff1a;只处理引用型数据&#xff1b;Object类中…

C语言图书管理系统注册功能,图书管理系统的c语言源程序

/*****************************************************************************************/#include #include #include #include /输入/输出文件流类using namespace std;const int maxr100;/最多的读者const int maxb100;/最多的图书const int maxbor5;/每位读者最多借…

python三层架构

conf/setting(配置文件) 一般是对utility进行相关设置index(主文件)main函数触发某个对象的业务逻辑方法model(数据库)admin 是对数据库的操作&#xff0c;数据库的增删改查操作utility(公共功能)sql_helper操作数据库的方法(其实就是些连接数据库&#xff0c;关闭数据库等…

【转】对random_state参数的理解

转自&#xff1a;https://blog.csdn.net/az9996/article/details/86616668 在学习机器学习的过程中&#xff0c;常常遇到random_state这个参数&#xff0c;下面来简单叙述一下它的作用。作用&#xff1a;控制随机状态。 原因&#xff1a;为什么需要用到这样一个参数random_stat…

关于项目总结一下

最近在做两很多事情&#xff0c;总结一下 1、MySQL使用需要注意的地方1) 存储引擎选择InnoDB&#xff0c;在高并发下读写有很好的表现2) 数据合理分表分区&#xff0c;均衡各数据库服务器的负载3) 适当作数据的冗余&#xff0c;便于在cache失效时的快速恢复 2、Redis使用需要注…

鸿蒙系统能内测吗,鸿蒙系统内测用户:使用体验已经超越ios

首先&#xff0c;鸿蒙流畅度堪比iOS&#xff0c;如同德芙巧克力一样非常丝滑&#xff0c;各种界面切换毫无卡顿。鸿蒙是万物互联的基础&#xff0c;在与其他设备联动上&#xff0c;使用了freebuds pro和magic wathc2&#xff0c;状态栏就能直接切换耳机&#xff0c;非常顺畅&am…

19.04.02笔记

用户权限&#xff1a;su 切换用户账户格式&#xff1a;【su 用户名 】【su】 切换到root【su root】 切换到root【su -】 切换到root用户 同时切换到root目录添加组&#xff1a;【groupadd 组名】 添加组 需要用户权限删除组【groupdel 组名】 删除组 需要用户权限和清空组…

VS2013自带的Browser Link功能引发浏览localhost网站时不停的轮询

浏览localhost网站时候不管你打开那个页面它都会不停的轮询。据悉这是VS2013自带的Browser Link功能&#xff0c;里面用到SignalR机制什么是Browser Link功能&#xff0c;什么是SignalR机制大家可以没事去百度了解一下。Browser Link功能讲解地址&#xff1a;http://www.cxyclu…

编译型语言和解释型语言(转载)

在具体计算机上实现一种语言&#xff0c;首先要确定的是表示该语言语义解释的虚拟计算机&#xff0c;一个关键的问题是程序执行时的基本表示是实际计算机上的机器语言还是虚拟机的机器语言。这个问题决定了语言的实现。根据这个问题的回答&#xff0c;可以将程序设计语言划分为…

魅族手机使用鸿蒙系统,魅族宣布接入华为鸿蒙系统,这应该是黄章最正确的决定...

安卓能有现在的成就一切源于苹果之外其它所有品牌都在使用&#xff0c;俗话讲“众人拾柴火焰高”就是这个道理。相对来讲华为鸿蒙要想做大做强必须有其它品牌支持才可以&#xff0c;如果华为自己一家使用是无法做到与苹果的iOS、谷歌的安卓相抗衡的。这就是为什么华为鸿蒙正式确…