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 = "/";