Asp.net, Cookie operations

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

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

// Write multi-value cookies

// 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

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.

Httpcookie cookie = new httpcookie ("userinfo1 ");
Cookie. expires = datetime. Now. adddays (-30 );
Response. Cookies. Add (cookie );


Modify cookie
1 response. Cookies ["info"] ["user"] = "2 ";
2 response. Cookies ["info"]. expires = datetime. Now. adddays (1); Delete attributes under cookies
1 httpcookie acookie = request. Cookies ["info"];
2 acookie. Values. Remove ("userid ");
3 acookie. expires = datetime. Now. adddays (1 );
4 response. Cookies. Add (acookie); Delete all cookies and set the expiration time to now.
1 int Limit = request. Cookies. Count-1;
2 For (INT I = 0; I <limit; I ++)
3 {
4 acookie = request. Cookies (I)
5 acookie. expires = datetime. Now. adddays (-1)
6 response. Cookies. Add (acookie)
7}

-------------

If you have a master site and a second-level domain name site and the Cookie needs to be shared, add the following settings:

Cookie. Domain = ". Primary Domain Name ";
Cookie. Path = "/";

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

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

// Write multi-value cookies

// 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

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.

Httpcookie cookie = new httpcookie ("userinfo1 ");
Cookie. expires = datetime. Now. adddays (-30 );
Response. Cookies. Add (cookie );


Modify cookie
1 response. Cookies ["info"] ["user"] = "2 ";
2 response. Cookies ["info"]. expires = datetime. Now. adddays (1); Delete attributes under cookies
1 httpcookie acookie = request. Cookies ["info"];
2 acookie. Values. Remove ("userid ");
3 acookie. expires = datetime. Now. adddays (1 );
4 response. Cookies. Add (acookie); Delete all cookies and set the expiration time to now.
1 int Limit = request. Cookies. Count-1;
2 For (INT I = 0; I <limit; I ++)
3 {
4 acookie = request. Cookies (I)
5 acookie. expires = datetime. Now. adddays (-1)
6 response. Cookies. Add (acookie)
7}

-------------

If you have a master site and a second-level domain name site and the Cookie needs to be shared, add the following settings:

Cookie. Domain = ". Primary Domain Name ";
Cookie. Path = "/";

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.