Cookie in ASP. NET: Method for creating a Cookie (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 in ASP. NET: Method for creating a Cookie (2)
HttpCookie aCookie = new HttpCookie ("lastVisit ");
// Last access time
ACookie. Value = DateTime. Now. ToString ();
ACookie. Expires = DateTime. Now. AddDays (1 );
Response. Cookies. Add (aCookie );
Cookie in ASP. NET: Access Cookie 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 );
}
Cookie in ASP. NET: 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 );
Cookie in ASP. NET: Method for creating multi-value cookies (2)
HttpCookie aCookie = new HttpCookie ("userInfo ");
ACookie. Values ["userName"] = "admin ";
ACookie. Values ["lastVisit"] = DateTime. Now. ToString ();
ACookie. Expires = DateTime. Now. AddDays (1 );
Response. Cookies. Add (aCookie );
Cookie in ASP. NET: Read multi-value Cookie
HttpCookie aCookie = Request. Cookies ["userInfo"];
String userName = aCookie. Values ["userName"];
String lastVisit = aCookie. Values ["lastVisit"];
Cookies in ASP. NET: 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.