Usage of cookies

Source: Internet
Author: User

1. Setting cookies is simple. There are two methods:

1. directly add the Cookie value:
Response. Cookies ["userName"]. value = "Tom ";
Response. Cookies ["userName"]. Expires = DateTime. Now. AddDays (1); \ expiration time, which cannot be viewed or called in the Cookies file.

2. Create an instance of the Cookie object:
HttpCookie cookie = new HttpCookie ("userName ");
Cookie. Value = "Tom ";
Cookie. Expires = DateTime. Now. AddDays (1 );
Response. Cookies. Add (aCookie)

You can use either method to generate a file with "userName". You can view it in your Temporary Internet folder.

You can also create and add Cookies with subkeys, such:
Response. Cookies ["userInfo"] ["userName"] = "Tom ";

Or:
HttpCookie cookie = new HttpCookie ("userInfo ");
Cookie. Values ["userName"] = "Tom ";
ACookie. Expires = DateTime. Now. AddDays (1 );
Response. Cookies. Add (aCookie)

Ii. Cookies retrieval:
A key value of Cookies is:
Server. HtmlEncode (Request. Cookies ["userInfo"] ["userName"])
You can use the Response. Write () method to output it to the page, such:
Response. Write (Server. HtmlEncode (Request. Cookies ["userInfo"] ["userName"]);

Or assign values to other variables:

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

You can use the Cookies [I] array to retrieve all items and subkeys, such:
String [] cooName = new string [Request. Cookies. Count];
String [] cooValue = new string [Request. Cookies. Count];
HttpCookie aCookie;
For (int I = 0; I <Request. Cookies. Count; I ++ ){
ACookie = Request. Cookies [I];
CooName [I] = Server. HtmlEncode (aCookie. Name );
If (! ACookie. HasKeys ){
CooValue [I] = Server. HtmlEncode (aCookie. Value );
} Else {
String [] subcooName = new string [aCookie. Values. Count];
String [] subcooValue = new string [aCookie. Values. Count];
For (int j = 0; j <aCookie. Values. Count; j ++ ){
SubcooName [j] = Server. HtmlEncode (aCookie. Values. AllKeys [j]);
SubcooValue [j] = Server. HtmlEncode (aCookie. Values [j]);
}
}
}

3. Modify Cookies
If it is a value-type Cookie value, such as the number of visits, you can read the value for addition and subtraction before saving it back. General changes can be saved directly to the new value, the system automatically overwrites the original value with the new value. The stored method is the same as the created method.

Iv. Delete Cookies
To delete Cookies, you only need to set the validity period to invalid. For example, you can set the validity period to one day at the time of creation:
Cookie. Expires = DateTime. Now. AddDays (1 );
To delete a file, set it:
Cookie. Expires = DateTime. Now. AddDays (-1 );

Delete subkeys:
HttpCookie;
Cookie = Request. Cookies ["userInfo"];
ACookie. Values. Remove ("userName ");
ACookie. Expires = DateTime. Now. AddDays (1 );

Related Article

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.