CookieHelper主要实现的功能介绍:
1,写cookie值 不在客户端创建文件,存放在服务器内存中
2,写cookie值 在客户端创建文件。存放cookie值(单一项值)
3,写cookie值(存放数组形式参数,用于第一次写cookie值,并指定有效时间)
4,写cookie值(操作已经存在的cookie,存放数组形式参数)
5,清除登录用户的cookie
6,读cookie值
7,获得cookie值

8,是否为有效域

CookieHelper源码


001using System;
002using System.Collections.Generic;
003using System.Linq;
004using System.Text;
005using System.Web;
006using System.Text.RegularExpressions;
007   
008namespace jizhuni.Common
009{
010    public class CookieHelper
011    {
012        /// <summary>
013        /// 写cookie值,不在客户端创建文件,存放在服务器内存中。
014        /// </summary>
015        /// <param name="strName">名称</param>
016        /// <param name="strValue">值</param>
017        public static void WriteCookie(string strName, string strValue)
018        {
019            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
020            if (cookie == null)
021            {
022                cookie = new HttpCookie(strName);
023            }
024            cookie.Value = strValue;
025            HttpContext.Current.Response.AppendCookie(cookie);
026   
027        }
028        /// <summary>
029        /// 写cookie值,在客户端创建文件。存放cookie值(单一项值)。
030        /// </summary>
031        /// <param name="strName">名称</param>
032        /// <param name="strValue">值</param>
033        /// <param name="cookieDomain">域</param>
034        /// <param name="expires">cookie 保存时长 单位分种</param>
035        public static void WriteCookie(string strName, string strValue, string cookieDomain, int expires)
036        {
037            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName);
038            cookie.Value = strValue;
039            //cookie.Expires = DateTime.Now.AddMinutes(expires);
040            cookie.Expires = DateTime.Now.AddMinutes(expires);
041            cookie.Domain = cookieDomain;
042            HttpContext.Current.Response.AppendCookie(cookie);
043   
044        }
045   
046        /// <summary>
047        /// 写cookie值(存放数组形式参数,用于第一次写cookie值,并指定有效时间)
048        /// </summary>
049        /// <param name="cookieName">cookies名字</param>
050        /// <param name="strValuesName">cookie项的名称,cookie[strValuesName][]</param>
051        /// <param name="strValue">cookie项的值,cookie[strValuesName][strValue]</param>
052        /// <param name="cookieDomain">cookie域属性</param>
053        /// <param name="expiresDays">cookies 有效时间 单位天</param>
054        public static void WriteCookie(string cookieName, string strValuesName, string strValue, string cookieDomain, int expiresDays)
055        {
056            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
057            if (cookie == null)
058            {
059                cookie = new HttpCookie(cookieName);
060                cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
061            }
062   
063            cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
064            cookie.Values["expires"] = expiresDays.ToString();
065            cookie.Expires = DateTime.Now.AddDays(expiresDays);
066   
067   
068            if (cookieDomain != string.Empty
069                && HttpContext.Current.Request.Url.Host.IndexOf(cookieDomain) > -1
070                && IsValidDomain(HttpContext.Current.Request.Url.Host))
071                cookie.Domain = cookieDomain;
072   
073            HttpContext.Current.Response.AppendCookie(cookie);
074        }
075   
076   
077        /// <summary>
078        /// 写cookie值(操作已经存在的cookie,存放数组形式参数)
079        /// </summary>
080        /// <param name="cookieName">cookies名字</param>
081        /// <param name="strValuesName">cookie项的名称,cookie[strValuesName][]</param>
082        /// <param name="strValue">cookie项的值,cookie[strValuesName][strValue]</param>
083        /// <param name="cookieDomain">cookie域属性</param>
084        public static void WriteCookie(string cookieName, string strValuesName, string strValue, string cookieDomain)
085        {
086            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
087            if (cookie == null)
088            {
089                cookie = new HttpCookie(cookieName);
090                cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
091            }
092            else
093            {
094   
095                cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
096   
097                var httpCookie = HttpContext.Current.Request.Cookies[cookieName];
098                if (httpCookie != null && httpCookie["expires"] != null)
099                {
100                    int intExpires = PublicMethod.GetInt(httpCookie["expires"], 0);
101                    if (intExpires > 0)
102                    {
103                        cookie.Values["expires"] = intExpires.ToString();
104                        cookie.Expires = DateTime.Now.AddMinutes(intExpires);
105                    }
106                }
107   
108            }
109   
110            if (cookieDomain != string.Empty &&
111                HttpContext.Current.Request.Url.Host.IndexOf(cookieDomain, System.StringComparison.Ordinal) > -1 &&
112                IsValidDomain(HttpContext.Current.Request.Url.Host))
113                cookie.Domain = cookieDomain;
114   
115            HttpContext.Current.Response.AppendCookie(cookie);
116   
117        }
118   
119        /// <summary>
120        /// 清除登录用户的cookie
121        /// </summary>
122        public static void ClearUserCookie(string cookieDomain, string cookiesName)
123        {
124            HttpCookie cookie = new HttpCookie(cookiesName);
125            cookie.Values.Clear();
126            cookie.Expires = DateTime.Now.AddYears(-1);
127            //string cookieDomain = ConfigFactory.GetConfig().CookieDomain.Trim();
128            if (cookieDomain != string.Empty &&
129                HttpContext.Current.Request.Url.Host.IndexOf(cookieDomain, System.StringComparison.Ordinal) > -1
130                && IsValidDomain(HttpContext.Current.Request.Url.Host))
131                cookie.Domain = cookieDomain;
132            HttpContext.Current.Response.AppendCookie(cookie);
133   
134        }
135   
136   
137        /// <summary>
138        /// 读cookie值
139        /// </summary>
140        /// <param name="strName">名称</param>
141        /// <returns>cookie值</returns>
142        public static string GetCookie(string strName)
143        {
144            var httpCookie = HttpContext.Current.Request.Cookies[strName];
145            return httpCookie != null ? httpCookie.Value : "";
146        }
147   
148        /// <summary>
149        /// 获得cookie值
150        /// </summary>
151        /// <param name="cookiesName">Cookie 名称</param>
152        /// <param name="strName">项</param>
153        /// <returns>值</returns>
154        public static string GetCookie(string cookiesName, string strName)
155        {
156            var httpCookie = HttpContext.Current.Request.Cookies[cookiesName];
157            if (httpCookie != null && httpCookie[strName] != null)
158            {
159                return HttpUtility.UrlDecode(httpCookie.Values[strName]);
160            }
161   
162            return "";
163        }
164   
165        /// <summary>
166        /// 是否为有效域
167        /// </summary>
168        /// <param name="host">域名</param>
169        /// <returns></returns>
170        public static bool IsValidDomain(string host)
171        {
172            Regex r = new Regex(@"^\d $");
173            if (host.IndexOf(".") == -1)
174            {
175                return false;
176            }
177            return !r.IsMatch(host.Replace(".", string.Empty));
178        }
179    }
180}

原文链接:C#操作Cookie方法大全 CookieHelper类库