usingSystem;usingSystem.Collections.Generic;usingsystem.web;namespacezhong.core{/// <summary> ///Cookie Operation class/// </summary> Public classCookiehelper {Private Static ReadOnly stringCookieName ="Zhong"; /// <summary> ///Set Cookies/// </summary> /// <param name= "name" >name</param> /// <param name= "values" >key/value pairs</param> /// <param name= "Expires" >Expiration Time-out (seconds), 0 does not set expiration</param> /// <param name= "domain" >Domain name</param> /// <param name= "path" >Path</param> Public Static voidSetcookie (stringName, dictionary<string,string> Values,intExpiresstringDomain =NULL,stringPath =NULL) {HttpCookie cookie=Httpcontext.current.response.cookies[name]; if(Cookie = =NULL) {Cookie=NewHttpCookie (name); } foreach(keyvaluepair<string,string> KVinchvalues) {cookie. Values.add (KV. Key, KV. Value); } if(Domain! =NULL) {cookie. Domain=domain; } if(Path! =NULL) {cookie. Path=path; } if(Expires! =0) {cookie. Expires= DateTime.Now.AddSeconds (expires);//Expiry Time} HttpContext.Current.Response.Cookies.Add (cookie); } /// <summary> ///Set Cookies/// </summary> /// <param name= "key" >Key</param> /// <param name= "value" >value</param> Public Static voidSetcookie (stringKeystringvalue) {Setcookie (CookieName,Newdictionary<string,string> {key, Value}},0); } /// <summary> ///read cookies by name and key/// </summary> /// <param name= "name" >name</param> /// <param name= "key" >Key</param> /// <returns></returns> Public Static stringGetCookie (stringNamestringkey) { stringReturnVal =NULL; HttpCookie Cookies=Httpcontext.current.request.cookies[name]; if(Cookie! =NULL) {ReturnVal=Cookie[key]; } returnReturnVal; } /// <summary> ///read cookies by key/// </summary> /// <param name= "key" >Key</param> /// <returns></returns> Public Static stringGetCookie (stringkey) { returnGetCookie (CookieName, key); } /// <summary> ///get a cookie by name/// </summary> /// <param name= "name" >name</param> /// <returns></returns> Public Static stringGetcookiebyname (stringname) { stringReturnVal =NULL; HttpCookie Cookies=Httpcontext.current.request.cookies[name]; if(cookie!=NULL) {ReturnVal=cookies. Value; } returnReturnVal; } /// <summary> ///Delete Cookies/// </summary> /// <param name= "name" >name</param> Public Static voidDeletecookie (stringname) {HttpCookie Cookie=Httpcontext.current.response.cookies[name]; if(Cookie! =NULL) {cookie. Expires= DateTime.Now.AddYears (-1); Cookies. Values.clear (); } HttpContext.Current.Response.Cookies.Add (cookie); } }}
Operation of. NET Cookies