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 );