Overview of ASP. NET cookie operations

Source: Internet
Author: User

The most basic ASP. NET cookie operations:

 
 
  1. HttpCookie cookie1 =NewHttpCookie ("User","Liuliang");
  2. Cookie1 ["Age"] ="18";
  3. Cookie1.Values. Add ("Sex","Male");
  4. Cookie1.Expires = DateTime. Now. AddHours (1 );
  5. Response. AppendCookie (cookie1 );
  6.  
  7. HttpCookie cookie =NewHttpCookie ("Luis");
  8. Cookie. Values. Add ("Name","Dangdang");
  9. Cookie ["Age"] ="23";
  10. Cookie ["Sex"] ="Man";
  11. Cookie. Expires = DateTime. Now. AddMinutes (0.5 );
  12. Response. Cookies. Add (cookie );

Then read:

 
 
  1. HttpCookie cookie = Request.Cookies["luis"];  
  2. if (null == cookie)  
  3.     return;  
  4. Response.Write(cookie.Value);  
  5. Response.Write("< br/>age" + cookie.Values.Count.ToString());  
  6. Response.Write("< br/>age" + cookie["name"]);  
  7. Response.Write("< br/>age" + cookie["age"]);  
  8. Response.Write("< br/>sex" + cookie.Values["sex"]);  
  9. Response.Write("< br/>sex" + cookie.Values[1]); 

Since cooike cannot be deleted directly on the server side, we only need to set it to expire.

 
 
  1. // Delete the cookie 
  2. HttpCookie cookie2 = Request. Cookies ["User"];
  3. If(Null= Cookie2)
  4. Return;
  5. Cookie2.Expires = DateTime. Now. AddDays (-1 );
  6. Response. AppendCookie (cookie2 );

Then we can:

Client to display cookies

The simplest code is:

 
 
  1. var c=document.cookie.toString();  
  2. alert(c);  

ASP. NET cookie Operation Method 2: You can also use the cookie operation:

Below is

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.