Introduction to 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)

 
 
  1. Response.Cookies["userName"].Value = “admin";  
  2. 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)

 
 
  1. HttpCookie aCookie =NewHttpCookie ("lastVisit ");// Last access time 
  2. ACookie. Value = DateTime. Now. ToString ();
  3. ACookie. Expires = DateTime. Now. AddDays (1 );
  4. Response. Cookies. Add (aCookie );

Cookie in ASP. NET: Access Cookie method (1)

 
 
  1. If(Request. Cookies ["UserName"]! =Null)
  2. Label1.Text = Server. HtmlEncode (Request. Cookies ["UserName"]. Value); Cookie Access Method (2)
  3. If(Request. Cookies ["UserName"]! =Null)
  4. {
  5. HttpCookie aCookie = Request. Cookies ["UserName"];
  6. Label1.Text = Server. HtmlEncode (aCookie. Value );
  7. }

Cookie in ASP. NET: Method for creating multi-value cookies (1)

 
 
  1. Response.Cookies["userInfo"]["userName"] = “admin";   
  2. Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();   
  3. Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1); 

Cookie in ASP. NET: Method for creating multi-value cookies (2)

 
 
  1. HttpCookie aCookie = new HttpCookie("userInfo");   
  2. aCookie.Values["userName"] = “admin";   
  3. aCookie.Values["lastVisit"] = DateTime.Now.ToString();   
  4. aCookie.Expires = DateTime.Now.AddDays(1);   
  5. Response.Cookies.Add(aCookie);  

Cookie in ASP. NET: Read multi-value Cookie

 
 
  1. HttpCookie aCookie = Request.Cookies["userInfo"];   
  2. string userName=aCookie.Values[“userName”];  
  3. 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.

  1. Implementation of ASP. NET form Authentication
  2. An error occurred while submitting the asp.net Form using JQuery Form Ajax.
  3. Summary of ASP. NET installation and deployment problems
  4. Experience in running ASP. NET on APACHE
  5. ASP. NET Online Learning Resource Summary

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.