Cookie (httpcookie instance) provides a web applicationProgramTo store user-specific information. For example, when a user accesses your site, you can use cookies to store user preferences or other information. When the user visits your website again, the application can retrieve the previously stored information.
Cookie creation method (1)
Response. Cookies ["Username"]. Value =" admin";Response. Cookies ["username"]. expires = datetime. Now. adddays (1 );
// If no expiration time is set, the cookie information will not be written to the user's hard disk, and will be discarded if the browser is closed.
Cookie creation method (2)
Httpcookie acookie =NewHttpcookie ("lastvisit ");// Last access timeAcookie. value = datetime. Now. tostring (); acookie. expires = datetime. Now. adddays (1); response. Cookies. Add (acookie );
Cookie Access Method (1)
If(Request. Cookies ["Username"]! =Null) Label1.text = server. htmlencode (request. Cookies ["Username"]. Value); Cookie Access Method (2)If(Request. Cookies ["Username"]! =Null) {Httpcookie acookie = request. Cookies ["Username"]; Label1.text = server. htmlencode (acookie. Value );}
Method for creating multi-value cookies (1)
Response. Cookies ["Userinfo"] ["Username"] =" Admin";Response. Cookies ["Userinfo"] ["Lastvisit"] = Datetime. Now. tostring (); response. Cookies ["Userinfo"]. Expires = datetime. Now. adddays (1 );
Method for creating multi-value cookies (2)
Httpcookie acookie =NewHttpcookie ("Userinfo"); Acookie. Values ["Username"] =" Admin";Acookie. Values ["Lastvisit"] = Datetime. Now. tostring (); acookie. expires = datetime. Now. adddays (1); response. Cookies. Add (acookie );
Read multi-value cookies
Httpcookie acookie = request. Cookies ["Userinfo"];StringUsername = acookie. Values ["username"];StringLastvisit = acookie. Values ["lastvisit"];
Modify and delete cookies
You cannot directly modify or delete a cookie. You can create only one new cookie and send it to the client to modify or delete the cookie.
Post address: http://dev.firnow.com/course/4_webprogram/asp.net/asp_netshl/2008430/112167.html