This section describes the basic usage of cookies.
1. COOKIE principles
When the client accesses the server for the first time
When the client accesses the server again
2. Create a COOKIE
First, reference the System. Web namespace
Code
// Create a COOKIE object
HttpCookie myCookie = new HttpCookie ("UserInfo ");
// Store the COOKIE value
MyCookie ["UserName"] = "UserName ";
MyCookie ["UserSex"] = "UserSex"; // set the domain name myCookie related to this COOKIE. domain = "cnblogs.com"; // set the COOKIE validity period. expires = DateTime. now. addDays (5 );
// Add a COOKIE to the current web Request
Response. Cookie. Add (myCookie );
3. Read COOKIE
HttpCookie myCookie = Resquest. Cookie ["UserInfo"];
String userName;
If (myCookie! = Null)
{
UserName = myCookie ["UserName"];
}
4. Cancel Cookie
HttpCookie myCookie = new HttpCookie ("UserInfo ");
MyCookie. Expires = DateTime. Now. AddDays (-1 );
Response. Cookies. Add (myCookie );
5. How to view local cookies
First, set the hidden file to visible.
Open C:/Documents and Settings/current user/Cookies/to view the COOKIE file!