. Net MVC Cookie Manipulation Encapsulation method

Source: Internet
Author: User

Do not be familiar with the cookie operation can refer to.

Generally, a cookie is something that stores data for a visitor's computer, can treat a cookie as a table, has a table name, can have many rows, has two fields in each row, and key and Value,key are the only ones that can find the corresponding value by key.


Encapsulation Method:


        #region Set cookies//<summary>//Set cookies///</summary>//<param N Ame= "key" > Key </param>//<param name= "value" > Value </param>///<param name= "CookieName" &G T;cookie name </param>//<param name= "cookiedays" >cookie reserved for how many days (default 3 days), you can use decimal notation </param> public s tatic void Setcookies (Httpresponsebase Response, Httprequestbase Request, string key, String value, String Cookiename,dou            ble cookiedays=3) {HttpCookie cookie = request.cookies[cookiename]; Cookies.            Expirationset = false;                if (cookie = = null) {cookie = new HttpCookie (cookiename); Cookies.            Values.add (key, value); } else {if (cookie.                Values[key]! = null) {Cookie[key] = value; } else cookie.            Values.add (key, value); } if(httpcontext.current! = null)                    {if (HttpContext.Current.Request.Url.DnsSafeHost.Contains ("kinpan.com")) { Cookies.                Domain = "kinpan.com"; }}//cookie.            Domain = configurationmanager.appsettings["Kinpandomain"]; Cookies.            Path = "/"; Cookies.            Expires = DateTime.Now.AddDays (cookiedays);            Response.Cookies.Remove (CookieName);        RESPONSE.COOKIES.ADD (cookie); } #endregion #region Delete cookies//<summary>///delete cookies, we do not have permission to delete them directly, but create a new cookie with the same name, set to        Browser will detect and delete cookies with the same name. </summary>//<param name= "cookiename" >cookie name </param> public static void Deletecook IE (httpresponsebase response,string cookiename) {if (cookiename.                HasValue ()) {HttpCookie acookie = new HttpCookie (cookiename); Acookie.expires = DateTime.Now.AddDays (-1);               RESPONSE.COOKIES.ADD (Acookie); }} #endregion #region get the entire cookie///<summary>//Get the entire cookie///&LT;/S ummary>//<param name= "CookieName" ></param>///<returns></returns> Publ IC Static HttpCookie GetCookie (Httprequestbase Request, string cookiename) {return httpcontext.current.        Request.cookies[cookiename]; #endregion #region Get the value of the key in the cookie//<summary>///Get the value of the key in the cookie.//&LT;/S        ummary>//<param name= "name" > Name </param>//<param name= "key" > Key </param> <returns> return key corresponding value </returns> public static string GetCookies (Httprequestbase Request, string name, St Ring key) {if (String.IsNullOrEmpty (name) | |            String.IsNullOrEmpty (key)) {return string.empty;       } else     {HttpCookie cookie = request.cookies[name]; if (cookie = null && cookie[key]! = null) {String value = Cookie[key].                    ToString ();                return HttpContext.Current.Server.UrlDecode (value);                } else {return string.empty; }}} #endregion


. Net MVC Cookie Manipulation Encapsulation method

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.