Asp.net various cookie code and parsing instances

Source: Internet
Author: User

Cookie is a piece of text information. Storing cookies on the client is one of the methods in which ASP. NET session States associate requests with sessions. Cookies can also be directly used to maintain data between requests, but the data will be stored on the client and sent to the server along with each request. The browser has a limit on the Cookie size. Therefore, the Cookie can be accepted only when the Cookie size does not exceed 4096 bytes.

Compile Cookie

Copy codeThe Code is as follows:
// Method 1:
Response. Cookies ["username"]. value = "mike ";
Response. Cookies ["username"]. Expires = DateTime. MaxValue;

// Method 2:
HttpCookie acookie = new HttpCookie ("last ");
Acookie. Value = "";
Acookie .. Expires = DateTime. MaxValue;
Response. Cookies. Add (acookie );


// Method 1:
Response. Cookies ["userinfo1"] ["name"]. value = "mike ";
Response. Cookies ["userinfo1"] ["last"]. value = "";
Response. Cookies ["userinfo1"]. Expires = DateTime. MaxValue;

// Method 2:
HttpCookie cookie = new HttpCookie ("userinfo1 ");
Cookie. Values ["name"] = "mike ";
Cookie. Values ["last"] = "";
Cookie. Expires = DateTime. MaxValue;
// Cookie. Expires = System. DateTime. Now. AddDays (1); // set the expiration time to 1 day.
Response. Cookies. Add (cookie );

Read Cookie
Internet Explorer saves site cookies in the file named <user >@< domain>. txt, where <user> is your account name.
Note: before obtaining the Cookie value, make sure that the Cookie exists. Otherwise, you will get an exception

Copy codeThe Code is as follows:
If (Request. Cookies ["userName"]! = Null)
{
String str = Request. Cookies ("userName"). Value;
}

// Read multi-value cookies
If (Request. Cookies ["userInfo1"]! = Null)
{
String name = Request. Cookies ["userInfo1"] ["name"];
String last = Request. Cookies ["userInfo1"] ["last"];
}


// Read the Cookie set
For (int I = 0; I <Request. Cookies. Count; I ++)
{
HttpCookie cookies = Request. Cookies;
Response. Write ("name =" + cookies. Mame + "<br/> ");
If (cookies. HasKeys) // whether a subkey exists
{
System. Collections. Specialized. NameValueCollection NameColl
= ACookie. Values;
For (int j = 0; j <NameColl. Count; j ++)
{
Response. Write ("sub-key name =" + NameColl. AllKey [j] + "<br/> ");
Response. Write ("sub-key value =" + NameColl [j] + "<br/> ");
}

}
Else
{
Response. Write ("value =" + cookies. Value + "<br/> ");
}
}

When you run this code, you can see a Cookie named "ASP. NET_SessionId". ASP. NET uses this Cookie to save the unique identifier of your session.

Modify Cookie
The modification method is the same as the creation method.

Delete Cookie
Set its validity period to a previous date. When the browser checks the Cookie validity period, the expired Cookie is deleted.

Copy codeThe Code is as follows:
HttpCookie cookie = new HttpCookie ("userinfo1 ");
Cookie. Expires = DateTime. Now. AddDays (-30 );
Response. Cookies. Add (cookie );


Modify cookie
Response. Cookies ["Info"] ["user"] = "2 ";
Response. Cookies ["Info"]. Expires = DateTime. Now. AddDays (1); // Delete the attributes under the cookie


HttpCookie acookie = Request. Cookies ["Info"];
Acookie. Values. Remove ("userid ");
Acookie. Expires = DateTime. Now. AddDays (1 );
Response. Cookies. Add (acookie); // delete all Cookies, that is, set the expiration time to now.


Int limit = Request. Cookies. Count-1;
For (int I = 0; I <limit; I ++)
{
Acookie = Request. Cookies (I)
Acookie. Expires = DateTime. Now. AddDays (-1)
Response. Cookies. Add (acookie)
}

If you have a master site and a second-level domain name site and the cookie needs to be shared, add the following settings:
Copy codeThe Code is as follows:
Cookie. Domain = ". Primary Domain Name"; // For example, .keleyi.com
Cookie. Path = "/";

Cookie. Expires AddDays (-1) is expired immediately

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.