A cookie is a piece of textual information in which the client store cookie is one of the methods that ASP.net session state will request to be associated. Cookies can also be used directly to persist data between requests, but the data is then stored on the client and sent to the server along with each request. The browser has a limit on the size of the Cookie, so only no more than 4096 bytes can be guaranteed to be accepted.
Writing cookies
Mode 1:
response.cookies["username"].value= "Mike";
response.cookies["username"]. Expires=datetime.maxvalue;
Mode 2:
HttpCookie acookie = new HttpCookie ("last");
Acookie. Value= "a";
Acookie.. Expires=datetime.maxvalue;
RESPONSE.COOKIES.ADD (Acookie);
The writing of multi-value cookies
Mode 1:
response.cookies["Userinfo1" ["Name"].value= "Mike";
response.cookies["Userinfo1" ["Last"].value= "a";
response.cookies["Userinfo1"]. Expires=datetime.maxvalue;
Mode 2:
HttpCookie cookie = new HttpCookie ("Userinfo1");
Cookie. values["Name"]= "Mike";
Cookie. values["Last"]= "a";
Cookie. Expires=datetime.maxvalue;
RESPONSE.COOKIES.ADD (cookie);
Read cookies
Internet Explorer saves the site's cookies in a file with a filename format of <user>@<domain>.txt, where <user> is your account name.
Note: You should ensure that the cookie does exist before you get the value of the cookie. Otherwise, you will get an exception
If (request.cookies["UserName"]!=null)
{
String str = request.cookies ("UserName"). Value;
}
Read multi-valued cookies
If (request.cookies["UserInfo1"]!=null)
{
String name=request.cookies["UserInfo1" ["Name"];
String last=request.cookies["UserInfo1" ["Last"];
}
Read Cookie Collection
for (int i = 0; I<request.cookies.count i++)
{
HttpCookie cookies = request.cookies[i];
Response.Write ("Name=" +cookies). Mame+ "<br>");
if (cookies). HasKeys)//whether there are subkeys
{
System.Collections.Specialized.NameValueCollection Namecoll
= Acookie.values;
for (int j=0;j<namecoll.count;j++)
{
Response.Write ("Subkey name =" + Namecoll.allkey[j] + "<br>");
Response.Write ("Subkey value =" + Namecoll[j] + "<br>");
}
}
Else
{
Response.Write ("value=" +cookies). Value+ "<br>");
}
}
When you run this code, you see a cookie,asp named "Asp.net_sessionid." NET uses this Cookie to save a unique identifier for your session.
modifying cookies
The modified method is the same as the creation method
Delete Cookies
Set its validity period to a date in the past. When the browser checks the validity of a cookie, it deletes the expired cookie.
HttpCookie cookie = new HttpCookie ("Userinfo1");
Cookie. Expires=datetime.now.adddays (-30);
RESPONSE.COOKIES.ADD (cookie);