C # set Cookies

Source: Internet
Author: User
1. Setting cookies is simple. There are two methods:
1. directly add the Cookie value:
Response. cookies ["userName"] = "Tom"; Response. cookies ["userName"]. expires = DateTime. now. addDays (1); \ expiration time, which cannot be viewed or called in Cookies.
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, set the validity period to one day at creation: cookie. Expires = DateTime. Now. AddDays (1 );
To delete a file, set it:
Cookie. Expires = DateTime. Now. AddDays (-1 );
Delete subkeys: HttpCookie cookie; cookie = Request. cookies ["userInfo"]; aCookie. values. remove ("userName"); aCookie. expires = DateTime. now. addDays (1); Response. cookies. add (aCookie );

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.