C # (Cookie) basic operations,

Source: Internet
Author: User

C # (Cookie) basic operations,

Create a new CookieHelper in Common and call it globally.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. web; namespace Common {public class CookieHelper {/// <summary> // set the cookie // </summary> /// <param name = "cookieName"> cookie name </ param> /// <param name = "cookieValue"> cookie value </param> /// <param name = "domain"> scope, if it is null, no write scope </param> public static void SetCookie (String cookieNam E, String cookieValue, string domain) {if (String. IsNullOrEmpty (cookieName) | String. IsNullOrEmpty (cookieValue) return; if (HttpContext. Current! = Null) {HttpCookie cookie = new HttpCookie (cookieName, cookieValue); if (domain. length> 0) {cookie. domain = domain;} cookie. httpOnly = true; HttpContext. current. response. cookies. add (cookie );}} /// <summary> /// set cookie /// </summary> /// <param name = "cookieName"> cookie name </param> /// <param name = "cookieValue"> cookie value </param> // <param name = "domain"> scope, if it is null, no scope is written. </param> // <param name = "day "> Validity period </param> public static void SetCookie (String cookieName, String cookieValue, string domain, int day) {if (String. isNullOrEmpty (cookieName) | String. isNullOrEmpty (cookieValue) return; if (HttpContext. current! = Null) {HttpCookie cookie = new HttpCookie (cookieName, cookieValue); if (domain. length> 0) {cookie. domain = domain;} cookie. httpOnly = true; cookie. expires = DateTime. now. addDays (day); HttpContext. current. response. cookies. add (cookie );}} /// <summary> /// set cookie expiration /// </summary> /// <param name = "cookieName"> name of the cookie to be expired </param> public static void ExpireCookie (String cookieName) {if (Strin G. IsNullOrEmpty (cookieName) return; if (HttpContext. Current! = Null) {HttpCookie cookie = new HttpCookie (cookieName, string. empty); cookie. httpOnly = true; cookie. expires = DateTime. now. addYears (-5); HttpContext. current. response. cookies. add (cookie );}} /// <summary> /// obtain the value of the corresponding Cookie name /// </summary> /// <param name = "cookieName"> Cookie name </param> /// <returns> </returns> public static string GetCookie (string cookieName) {if (string. isNullOrEmpty (cookieName) return string. empty; if (System. web. httpContext. current = null) return string. empty; if (System. web. httpContext. current. request. cookies [cookieName] = null) return string. empty; else return System. web. httpContext. current. request. cookies [cookieName]. value ;} /// <summary> /// determine whether the corresponding Cookie exists // </summary> /// <param name = "cookieName"> Cookie name </param> /// <returns> </returns> public static bool ExistCookie (string cookieName) {if (string. isNullOrEmpty (cookieName) | System. web. httpContext. current = null) return false; if (System. web. httpContext. current. request. cookies [cookieName] = null) return false; if (System. web. httpContext. current. request. cookies [cookieName]. value = null) return false; return (System. web. httpContext. current. request. cookies [cookieName]. value. length> 0 );}}}

  

 

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.