C#/asp. NET operation Cookie (read-write) code example

Source: Internet
Author: User


The cookie is present on the hard disk, ie the place where the cookie is stored is different from the place where Firefox stored the cookie. Different operating systems may not have the same place to store cookies.

different browsers store cookies in their own separate spaces and do not interfere with each other . in my Windows7, IE8, for example, a cookie exists this:

C:\Users\xiaoj\AppData\Local\Microsoft\Windows\Temporary Internet Files

Note: Cached files and cookie files are present together, both in this directory.

You can also find, open IE, click tools->internet options->general Tab under the setting button under thebrowsing history , click View files in the dialog box that pops up . Different websites will have different cookie files.

In ASP. NET, the read-write cookie is done by using the HttpCookie class, which is defined as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 publicsealed classHttpCookie {      // 获取或设置将此 Cookie 与其关联的域。默认值为当前域。      publicstringDomain {  get set ; }      // 获取或设置此 Cookie 的过期日期和时间(在客户端)。      publicDateTime Expires {  get set ; }      // 获取一个值,通过该值指示 Cookie 是否具有子键。      publicboolHasKeys {  get ; }      // 获取或设置一个值,该值指定 Cookie 是否可通过客户端脚本访问。      // 如果 Cookie 具有 HttpOnly 属性且不能通过客户端脚本访问,则为 true;否则为 false。默认为 false。      publicboolHttpOnly {  get set ; }      // 获取或设置 Cookie 的名称。      publicstringName {  get set ; }      // 获取或设置要与当前 Cookie 一起传输的虚拟路径。默认值为当前请求的路径。      publicstringPath {  get set ; }      // 获取或设置一个值,该值指示是否使用安全套接字层 (SSL)(即仅通过 HTTPS)传输 Cookie。      public boolSecure {  get set ; }      // 获取或设置单个 Cookie 值。默认值为空引用。      publicstringValue {  get set ; }      // 获取单个 Cookie 对象所包含的键值对的集合。      publicNameValueCollection Values {  get ; }      // 获取 System.Web.HttpCookie.Values 属性的快捷方式。      publicstringthis [ stringkey] {  get set ; } }


The process by which a cookie is written to the browser:

We can use the following code to write a cookie in the ASP and send it to the client's browser (for the sake of simplicity I have not set other properties).

1 2 HttpCookie cookie =  new  HttpCookie( "MyCookieName" "string value" ); Response.Cookies.Add(cookie);


The process of obtaining a cookie by asp:

We can use the following code to read a cookie in an ASP.

1 2 3 4 5 Ht Tpcookie cookie = request.cookies[ "Mycookiename" ]; if (cookie!=  null   )      Labcookie1.text = cookie. Value; Else      Labcookie1.text =  Not defined ;

The cookie is placed in the request header and sent to the server. If you refresh the page all the time, you will find that every HTTP request, the cookie will be sent. Of course, the browser does not send all the cookies it receives, it checks the domain name and directory currently being requested, as long as the two items match the domain and path that the cookie corresponds to. For domain, it is based on the principle of tail matching. Therefore, when I visit www.09me.com, the browser does not send out the cookies I receive when I browse www.169it.com.


To delete a cookie:

In fact, when writing a cookie, set expires to a "time earlier than the present time". That is: Set this cookie to expire and delete it when the browser receives the cookie.

1 2 3 HttpCookie cookie =  new  HttpCookie( "MyCookieName" null  ); cookie.Expires =  new DateTime(1900, 1, 1); Response.Cookies.Add(cookie);

Steps for browsers to answer cookies in the header of a Web server

1. Extract all cookies from the reply header of the Web server.

2. Parse the components of these cookies (name, value, path, etc.).

3. Determine if the host is allowed to set these cookies. If allowed, these cookies are stored locally.


Steps for the browser to filter all cookies in the Web server request header:

1. Depending on the URL of the request and the properties of the local store cookie, the cookie can be sent to the Web server.

2. For multiple cookies, determine the order in which they are sent.

3. Add the cookie that needs to be sent to the request HTTP header.





Reprint Please specify: The article is reproduced from: [169it-latest and most comprehensive it information]
This article is entitled: C#/asp. NET operation Cookie (read-write) code example


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.