Cookie read/write methods in ASP. NET

Source: Internet
Author: User
Cookie (HttpCookie instance) provides a method to store user-specific information in Web applications. 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 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 the browser will be discarded if it is disabled.
 

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); Cookies 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"]; cookie in ASP. NET: Modify and delete Cookie
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.
This post comes from the group music community-http://q.yesky.com/group/review-18114556.html

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.