The method of setting up cookies is very simple, there are two ways to do this:
1, add the cookie value directly:
response.cookies["userName"] = "Tom";
response.cookies["UserName"]. Expires = DateTime.Now.AddDays (1); \ expires, cannot be viewed in the cookie file, nor can it be invoked.
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)
In either of these ways, you can generate a file with a "UserName" item, which you can view in your Internet Temp folder.
Cookies can also be created and added with subkeys, such as:
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)
Second, the search cookies:
The value of a key of a cookie is:
Server.HTMLEncode (request.cookies["UserInfo"] ["UserName"])
You can output it to a page using the Response.Write () method, such as:
Response.Write (Server.HTMLEncode (request.cookies["UserInfo"] ["userName"]);
or assign to another variable:
String Strcookie1=server.htmlencode (request.cookies["UserInfo"] ["userName"]);
Use the Cookies[i] array to retrieve all items and subkeys, such as:
Copy Code code as follows:
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]);
}
}
}
Third, modify cookies
If it is a numeric type of cookie value, such as the number of visits, you can read the value of the addition and subtraction operation to save back, the general changes directly into the new value can be, the system automatically overwrites the original value with the new value, stored in the same method as the creation.
Iv. Deletion of cookies
Delete Cookies as long as the validity of the expiration of the can be set, such as at the time of creation of a day of validity:
Cookie. Expires = DateTime.Now.AddDays (1);
To delete, set to:
Cookie. Expires = DateTime.Now.AddDays (-1);
Delete subkey:
Copy Code code as follows:
HttpCookie cookies;
Cookie = request.cookies["UserInfo"];
ACookie.Values.Remove ("UserName");
Acookie.expires = DateTime.Now.AddDays (1);
RESPONSE.COOKIES.ADD (Acookie);