C # Action Cookie

Source: Internet
Author: User

Create a cookie

HttpCookie cookie =NewHttpCookie ("Mycook");//Initial and set the name of the cookieDateTime dt =DateTime.Now; TimeSpan TS=NewTimeSpan (1,0,0,0,0);//Expiration Time is 1 daysCookies. Expires = dt. ADD (TS);//Set Expiration TimeCookies. Values.add ("UID", UID);//Add a cookieResponse.appendcookie (cookie);

Read cookies

         if (request.cookies["mycook"null)            {                viewdata["  UID"] = request.cookies["mycook"["uid " // output Cookie            }

Modify Cookies

   //Gets the cookie object for the clientHttpCookie cookie= request.cookies["Mycook"]; if(cookie!=NULL) {//two ways to modify cookiesCookies. values["UID"] ="134626655"; Cookies. Values.set ("userid","134626655"); //Add new content to cookiesCookies. Values.set ("newid","NewValue");    Response.appendcookie (cookie); }      

Delete Cookies

HttpCookie cookie= request.cookies["Mycook"]; if(cookie!=NULL) {if(!checkbox1.checked) {cookie. Values.remove ("UID");//remove a value with a key value of UID}Else{TimeSpan TS=NewTimeSpan (-1,0,0,0); Cookies. Expires= DATETIME.NOW.ADD (TS);//Delete the entire cookie, just set the expiration time to now} response.appendcookie (cookie); }

Set a timespan for the time that the cookie expires

[Serializable]  Public TimeSpan (   int days  ,   int  hours,   int  Minutes,   int  seconds);

C # Action Cookie

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.