Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. web; <br/> namespace lib <br/> {<br/> public class cookieshelper <br/>{< br/> # region obtains the cookie <br/> // <Summary> <br/> // obtain the cookie value <br/> /// </Summary> <br/> // <Param name = "cookiename"> cookie name </param> <br/> // <returns> cookie value </returns> <br/> Public static string getcookievalue (string cookiename) <Br/>{< br/> return getcookievalue (cookiename, null ); <br/>}< br/> /// <summary> <br/> // obtain the cookie key value <br/> /// </Summary> <br />/// <Param name = "cookiename"> cookie name </param> <br/> /// <Param name = "key"> key </param> <br/> // <returns> cookie key value </returns> <br/> Public static string getcookievalue (string cookiename, string key) <br/>{< br/> httprequest request = httpcontext. current. request; <br /> If (request! = NULL) <br/>{< br/> return getcookievalue (request. cookies [cookiename], key); <br/>}< br/> return ""; <br/>}< br/> /// <summary> <br/> // obtain the Cookie's sub-key value <br/> /// </Summary> <br/> // <Param name = "cookie"> cookie name </param> <br/> // <Param name = "key"> key </param> <br/> // <returns> value corresponding to the cookie key </returns> <br/> Private Static string getcookievalue (httpcookie cookie, string key) <br/>{< br/> If (cookie! = NULL) <br/>{< br/> If (! String. isnullorempty (key) & cookie. haskeys) <br/>{< br/> return cookie. values [Key]; <br/>}< br/> else <br/>{< br/> return cookie. value; <br/>}< br/> return ""; <br/>}< br/> /// <summary> <br/> // obtain the cookie <br/> /// </Summary> <br/>/ // <Param name = "cookiename"> cookie name </param> <br/> // <returns> </returns> <br/> Public static httpcookie getcookie (string cookiename) <br/>{< br/> HTTP Request request = httpcontext. Current. Request; <br/> If (request! = NULL) <br/>{< br/> return request. cookies [cookiename]; <br/>}< br/> return NULL; <br/>}< br/> # endregion <br/> # region Delete cookie <br/> /// <summary> <br/> // Delete cookie <br />/// </Summary> <br/> /// <Param name = "cookiename"> cookie name </param> <br/> Public static void removecookie (string cookiename) <br/>{< br/> removecookie (cookiename, null ); <br/>}< br/> /// <summary> <br/> // Delete the cookie subkey <br />/// </Summary> <br/> /// <Param name = "cookiename"> cookie name </param> <br/> // <Param name = "key"> key </param> <br/> Public static void removecookie (string cookiename, string key) <br/>{< br/> httpresponse response = httpcontext. current. response; <br/> If (response! = NULL) <br/>{< br/> httpcookie cookie = response. Cookies [cookiename]; <br/> If (cookie! = NULL) <br/>{< br/> If (! String. isnullorempty (key) & cookie. haskeys) <br/>{< br/> cookie. values. remove (key); <br/>}< br/> else <br/>{< br/> response. cookies. remove (cookiename ); <br/>}< br/> # endregion <br/> # region setting/modifying cookies <br/> /// <summary> <br/> // set the cookie key and value, the default expiration time is 1 year <br/> /// </Summary> <br/> /// <Param name = "cookiename"> cookie name </param> <br/> /// <Param name = "key"> key </param> <br/>/ // <Param name = "value"> value </param> <br/> Public static void setcookie (string cookiename, string key, string value) <br/>{< br/> setcookie (cookiename, key, value, datetime. now. adddays (365 )); <br/>}< br/> /// <summary> <br/> // set the cookie expiration time <br/> /// </Summary> <br/> /// <Param name = "cookiename"> cookie name </param> <br/> /// <Param name = "expires"> expiration time </param> <br/> Public static void setcookieexpires (String cookiename, datetime expires) <br/>{< br/> setcookie (cookiename, null, null, expires ); <br/>}< br/> /// <summary> <br/> // set the cookie <br/> /// </Summary> <br/>/ // <Param name = "cookiename"> cookie name </param> <br/> // <Param name = "key"> key </param> <br/> /// <Param name = "value"> value </param> <br/> /// <Param name = "expires"> expiration time </param> <br/> Public static void setcookie (string cookiename, string K Ey, string value, datetime expires) <br/>{< br/> httpresponse response = httpcontext. Current. response; <br/> If (response! = NULL) <br/>{< br/> httpcookie cookie = response. Cookies [cookiename]; <br/> If (cookie! = NULL) <br/>{< br/> If (! String. isnullorempty (key) & cookie. haskeys) <br/>{< br/> cookie. values. set (key, value); <br/>}< br/> else <br/>{< br/> If (! String. isnullorempty (value) <br/>{< br/> cookie. value = value; <br/>}< br/> If (expires! = NULL) <br/>{< br/> cookie. expires = expires; <br/>}< br/> response. setcookie (cookie ); <br/>}< br/> # endregion <br/> # Add a cookie to region <br/> // <summary> <br/> // Add as Cookie. values set <br/> /// </Summary> <br/> /// <Param name = "cookiename"> </param> <br/> // <param name = "key"> </param> <br/> // <Param name = "value"> </param> <br/> Public static void addcookie (string cookiename, str Ing key, string value) <br/>{< br/> httpcookie = new httpcookie (cookiename); <br/> cookie. values. add (Key, value); <br/> addcookie (cookie ); <br/>}< br/> /// <summary> <br/> /// add as Cookie. values set <br/> /// </Summary> <br/> // <Param name = "cookiename"> cookie name </param> <br/> // /<Param name = "key"> key </param> <br/> // <Param name = "value"> value </param> <br/> // /<Param name = "expires"> expiration time </param> <Br/> Public static void addcookie (string cookiename, string key, string value, datetime expires) <br/>{< br/> httpcookie cookie = new httpcookie (cookiename ); <br/> cookie. expires = expires; <br/> cookie. values. add (Key, value); <br/> addcookie (cookie ); <br/>}< br/> /// <summary> <br/> // Add cookie <br/> /// </Summary> <br/>/ // <Param name = "cookie"> </param> <br/> Private Static void addcookie (HT Tpcookie) <br/>{< br/> httpresponse response = httpcontext. Current. response; <br/> If (response! = NULL) <br/>{< br/> // specifies whether the client script can access [false by default] <br/> cookie. HTTPOnly = true; <br/> // specify the unified path, and the cookie can be stored. <br/>. path = "/"; <br/> // set cross-domain, so that all other second-level domain names can access <br/> // cookie. domain = "chinesecoo.com"; <br/> response. appendcookie (cookie); <br/>}< br/> # endregion <br/>}< br/>}