Using system;
Using system. collections. generic; using system. LINQ; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. data; using system. configuration; namespace cookiescontrol {// <summary> // cookie operation class // </Summary> public class cookie {// <summary> // save a cookie /// </Summary> // <Param name = "cookiename"> coo Kie name </param> /// <Param name = "cookievalue"> cookie value </param> /// <Param name = "cookietime"> cookie expiration time (hours), 0 indicates that the page is disabled. </param> Public static void savecookie (string cookiename, string cookievalue, double cookietime) {httpcookie mycookie = new httpcookie (cookiename); datetime now = datetime. now; mycookie. value = cookievalue; If (cookietime! = 0) {// There are two methods. If you set the cookie time in the first method, disabling the browser will not automatically clear the cookie. // if you do not set the cookie time in the second method, when the browser is closed, the cookie is automatically cleared, but the validity period is not confirmed yet. Mycookie. expires = now. adddays (cookietime); If (httpcontext. Current. response. Cookies [cookiename]! = NULL) httpcontext. current. response. cookies. remove (cookiename); httpcontext. current. response. cookies. add (mycookie);} else {If (httpcontext. current. response. cookies [cookiename]! = NULL) httpcontext. current. response. cookies. remove (cookiename); httpcontext. current. response. cookies. add (mycookie );}} /// <summary> /// obtain cookievalue // </Summary> /// <Param name = "cookiename"> cookie name </param> /// <returns> cookie value </returns> Public static string getcookie (string cookiename) {httpcookie mycookie = new httpcookie (cookiename); mycookie = httpcontext. current. request. cookies [cookies Name]; If (mycookie! = NULL) return mycookie. value; else return NULL ;} /// <summary> /// clear cookievalue // </Summary> /// <Param name = "cookiename"> cookie name </param> Public static void clearcookie (string cookiename) {httpcookie mycookie = new httpcookie (cookiename); datetime now = datetime. now; mycookie. expires = now. addyears (-2); httpcontext. current. response. cookies. add (mycookie );}}}