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關(guān)聯(lián)的域</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關(guān)聯(lián)的域</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工具類