Sample Code for reading and writing cookie data in ASP. NET

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.