Server operation cookie [2]

Source: Internet
Author: User

The following three classes are used to operate cookies on the server:

Httpcookie, httpresponse, httprequest

Httpcookie:

Attribute Description Example
Domain Obtain or set the domain associated with this cookie  
Expires Get or set the domain expiration time  
Haskeys Obtains a value that indicates whether the cookie has a subkey.  
HTTPOnly Gets or sets a value that specifies whether a cookie can be accessed through a client script.  
Item Httpcookie. Values attributes shortcut  
Name Get or set the cookie name  
Path Obtains or sets the virtual path to be transmitted with the current cookie.  
Secure Gets or sets a value indicating whether to use SSL to transmit cookies.  
Value Obtains or sets the cookie value.  
Values Obtains the set of key-value pairs contained in a cookie object. Cookie. Values ["username"]
  1. Add cookie

     Public Partial Class Cookie_default: system. Web. UI. Page { Protected   Void Page_load ( Object Sender, eventargs e ){ // Create a cookie Httpcookie cookie = New Httpcookie (" User "); // Encode Chinese Characters Cookie [" Username "] = Httputility. urlencode (" Zhang San "); Cookie [" Password "] =" Zhangsan "; // Note: the cookie cannot be added if it is missing Cookie. expires = datetime. Now. adddays (2 ); // Add cookie Response. Cookies. Add (cookie );}}
  2. Read cookie
      Public  partial  class  cookie_default: system. web. UI. page { protected   void  page_load ( Object  sender, eventargs e) { // obtain cookie  httpcookie cookie = request. cookies [" User "];  // decode Chinese   // output:   // zhangsan   // zhangsan  response. write (httputility. urldecode (cookie [" username "]) + " 
    " + Cookie [" password "]);}
  3. modify cookie
      Public  partial  class  cookie_default: system. web. UI. page { protected   void  page_load ( Object  sender, eventargs e) {response. cookies [" User "] [" username "] = httputility. urlencode (" Li Si ");  // decode Chinese   // output:   // Li Si  response. write (httputility. urldecode (request. cookies [" User "] [" username "]) ;}
  4. Delete cookie
      Public  partial  class  cookie_default: system. web. UI. page { protected   void  page_load ( Object  sender, eventargs e) { // when the cookie expires, the client can automatically delete the cookie  response. cookies [" User "]. expires = datetime. now. adddays (-1) ;}}

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.