Operation of. NET Cookies

Source: Internet
Author: User

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

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.