Cookie操作帮助类 C#基类库CookieHelper工具类


将指定的Cookie添加到Cookie集合中

增加插入Cookie

清除指定的Cookie

 获取Cookie里某个key的值


CookieHelper帮助类库源码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;

namespace JsonsTeamUtil.Helper
{
    /// <summary>
    /// Cookie操作帮助类
    /// </summary>
    public class CookieHelper
    {
        /// <summary>
        /// 将指定的cookie添加到cookie集合中
        /// </summary>
        /// <param name="cookieName">要添加的cookie名称</param>
        /// <param name="coll">要添加的cookie的键值集合</param>
        /// <param name="expires">此cookie过期时间</param>
        /// <param name="domain">与此cookie关联的域</param>
        /// <param name="path">虚拟路径</param>
        /// <param name="httpOnly">指定 Cookie 是否可通过客户端脚本访问</param>
        public static void Add(string cookieName, NameValueCollection coll, DateTime expires,string domain,string 

path="/",bool httpOnly=false)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Values.Add(coll);
            cookie.Expires = expires;
            cookie.Path = path;
            cookie.Domain = domain;
            cookie.HttpOnly = httpOnly;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        public static void Add(string cookieName,string cookieValue, DateTime expires, string domain, string path = "/", 

bool httpOnly = false)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Value = cookieValue;
            cookie.Expires = expires;
            cookie.Path = path;
            cookie.Domain = domain;
            cookie.HttpOnly = httpOnly;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 清除指定的cookie
        /// </summary>
        /// <param name="cookieName">要清除的cookie</param>
        /// <param name="domain">与此cookie关联的域</param>
        /// <param name="path">虚拟路径</param>
        public static void Clear(string cookieName, string domain, string path = "/")
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-30);
                cookie.Path =path;
                cookie.Value = "";
                cookie.Domain =domain;
                HttpContext.Current.Response.AppendCookie(cookie);
            }
        }
        /// <summary>
        /// 获取cookie里某个key的值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="key">key键</param>
        /// <returns></returns>
        public static string Get(string cookieName, string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                string cookieValue = cookie.Values.Get(key);
                return cookieValue;
            }
            return string.Empty;
        }
        public static string Get(string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                return cookie.Value;
            }
            return string.Empty;
        }
    }
}


原文链接:Cookie操作帮助类 C#基类库CookieHelper工具类