Cookie for status management

Source: Internet
Author: User
1. Cookie Workflow

If there is not only a page in the user request site, but also a Cookie containing the expiration time, the user's browser also obtains the Cookie while obtaining the page, and store it in a folder on the user's hard disk.

Later, if the user requests the site again, the browser will find the Cookie associated with it on the local hard disk. If the Cookie exists, the browser sends the Cookie along with the page request to the requesting site.

Ii. Cookie-related rules

The browser manages cookies in the user's system. Cookie is sent to the browser through the HttpResponse object, which is known as a set of Cookies. The HttpResponse object can be accessed as the Response attribute of the Page class. All cookies to be sent to the browser must be added to this collection. When creating a Cookie, you must specify the Name and Value. Each Cookie must have a unique name so that it can be recognized when being read from the browser. Because cookies are stored by name, naming two cookies with the same name causes one Cookie to be overwritten.

3. Cookie operations

1: add a single-value Cookie to the Cookies set

Response. Cookies ["userName"]. Value = "patrick ";

Response. Cookies ["userName"]. Expires = DateTime. Now. AddDays (1d );

 

HttpCookie aCookie = new HttpCookie ("lastVisit ");

ACookie. Value = DateTime. Now. ToString ();

ACookie. Expires = DateTime. Now. AddDays (1d );

Response. Cookies. Add (aCookie );

2: Add multi-value Cookies to Cookies

Response. Cookies ["userInfo"] ["userName"] = "patrick ";

Response. Cookies ["userInfo"] ["lastVisit"] = DateTime. Now. ToString ();

Response. Cookies ["userInfo"]. Expires = DateTime. Now. AddDays (1 );

 

HttpCookie aCookie = new HttpCookie ("userInfo ");

ACookie. Values ["userName"] = "patrick ";

ACookie. Values ["lastVisit"] = DateTime. Now. ToString ();

ACookie. Expires = DateTime. Now. AddDays (1 );

Response. Cookies. Add (aCookie );

3: Read the Cookie value of a single value

If (Request. Cookies ["userName"]! = Null)

Label1.Text = Server. HtmlEncode (Request. Cookies ["userName"]. Value );

 

If (Request. Cookies ["userName"]! = Null)

{

HttpCookie aCookie = Request. Cookies ["userName"];

Label1.Text = Server. HtmlEncode (aCookie. Value );

}

4: Read multi-value Cookie values

If (Request. Cookies ["userInfo"]! = Null)

{

Label1.Text =

Server. HtmlEncode (Request. Cookies ["userInfo"] ["userName"]);

 

Label2.Text =

Server. HtmlEncode (Request. Cookies ["userInfo"] ["lastVisit"]);

}

5. delete a cookie

If (Request. Cookies ["UserSettings"]! = Null)

{

HttpCookie myCookie = new HttpCookie ("UserSettings ");

MyCookie. Expires = DateTime. Now. AddDays (-1d );

Response. Cookies. Add (myCookie );

}

 

6. display all cookies

 

Why does the Enumerator of HttpCookieCollection only traverse keys? I think this is mainly due to performance considerations. Returning A Key is more efficient than returning a Cookie object.

You expect to return the Cookie object when performing an Enumerate operation on Cookies. However, if Enumerator returns the Cookie, it needs to perform a new Cookie () operation, which means an object will be copied. Some Cookie objects are relatively large, and the copy efficiency is very low.

Cookies can be traversed as follows:
Foreach (string key in Request. Cookies)
{
Cookie cookie = Request. Cookie [key]; // no new Cookie object is generated, which is more efficient.

// Your code
} Reply to reference

I thought about it again and found that HTTPCookieCollection can be accessed through indexes. The best Traversal method should be for instead of foreach:

For (int I = 0; I <Request. Cookies. Count; I ++)
{
HttpCookie cookie = Request. Cookies [I];

//......
}

 

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.