CookieHelper

using System.Web:

 /// <summary>
    /// CookieHelper
    /// </summary>
    public static class CookieHelper
    {
        /// <summary>
        /// Cookies赋值
        /// </summary>
        /// <param name="strName">主键</param>
        /// <param name="strValue">键值</param>
        /// <param name="strDay">有效分钟</param>
        /// <returns></returns>
        public static bool SetCookie(string strCookieName, string strCookieValue, int intMin)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(strCookieName); //创建一个cookie对象  
                cookie.Value = strCookieValue; //设置cookie的值  
                cookie.Expires = DateTime.Now.AddMinutes(intMin); //或cookie.Expires.AddDays(intDay);设置cookie的有效期  
                HttpContext.Current.Response.Cookies.Add(cookie); //将cookie添加到cookies中  
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>

        public static string GetCookie(string strCookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strCookieName];//获取cookie  
            if (cookie != null)
            {
                return cookie.Value; //返回cookie的值  
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>
        public static bool delCookie(string strName)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(strName);
                //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
                Cookie.Expires = DateTime.Now.AddMinutes(-1);
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
原文地址:https://www.cnblogs.com/MrZheng/p/8966149.html