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