C #. NET Cookies Setup tips for Web pages-practical tips

Source: Internet
Author: User
Tags datetime
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);

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.