CookieHelper主要实现的功能介绍:
1,写cookie值 不在客户端创建文件,存放在服务器内存中
2,写cookie值 在客户端创建文件。存放cookie值(单一项值)
3,写cookie值(存放数组形式参数,用于第一次写cookie值,并指定有效时间)
4,写cookie值(操作已经存在的cookie,存放数组形式参数)
5,清除登录用户的cookie
6,读cookie值
7,获得cookie值
8,是否为有效域
CookieHelper源码
002 | using System.Collections.Generic; |
006 | using System.Text.RegularExpressions; |
008 | namespace jizhuni.Common |
010 | public class CookieHelper |
013 | /// 写cookie值,不在客户端创建文件,存放在服务器内存中。 |
015 | /// <param name="strName">名称</param> |
016 | /// <param name="strValue">值</param> |
017 | public static void WriteCookie( string strName, string strValue) |
019 | HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; |
022 | cookie = new HttpCookie(strName); |
024 | cookie.Value = strValue; |
025 | HttpContext.Current.Response.AppendCookie(cookie); |
029 | /// 写cookie值,在客户端创建文件。存放cookie值(单一项值)。 |
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) |
037 | HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName); |
038 | cookie.Value = strValue; |
040 | cookie.Expires = DateTime.Now.AddMinutes(expires); |
041 | cookie.Domain = cookieDomain; |
042 | HttpContext.Current.Response.AppendCookie(cookie); |
047 | /// 写cookie值(存放数组形式参数,用于第一次写cookie值,并指定有效时间) |
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) |
056 | HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; |
059 | cookie = new HttpCookie(cookieName); |
060 | cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue); |
063 | cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue); |
064 | cookie.Values[ "expires" ] = expiresDays.ToString(); |
065 | cookie.Expires = DateTime.Now.AddDays(expiresDays); |
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; |
073 | HttpContext.Current.Response.AppendCookie(cookie); |
078 | /// 写cookie值(操作已经存在的cookie,存放数组形式参数) |
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) |
086 | HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; |
089 | cookie = new HttpCookie(cookieName); |
090 | cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue); |
095 | cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue); |
097 | var httpCookie = HttpContext.Current.Request.Cookies[cookieName]; |
098 | if (httpCookie != null && httpCookie[ "expires" ] != null ) |
100 | int intExpires = PublicMethod.GetInt(httpCookie[ "expires" ], 0); |
103 | cookie.Values[ "expires" ] = intExpires.ToString(); |
104 | cookie.Expires = DateTime.Now.AddMinutes(intExpires); |
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; |
115 | HttpContext.Current.Response.AppendCookie(cookie); |
122 | public static void ClearUserCookie( string cookieDomain, string cookiesName) |
124 | HttpCookie cookie = new HttpCookie(cookiesName); |
125 | cookie.Values.Clear(); |
126 | cookie.Expires = DateTime.Now.AddYears(-1); |
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); |
140 | /// <param name="strName">名称</param> |
141 | /// <returns>cookie值</returns> |
142 | public static string GetCookie( string strName) |
144 | var httpCookie = HttpContext.Current.Request.Cookies[strName]; |
145 | return httpCookie != null ? httpCookie.Value : "" ; |
151 | /// <param name="cookiesName">Cookie 名称</param> |
152 | /// <param name="strName">项</param> |
153 | /// <returns>值</returns> |
154 | public static string GetCookie( string cookiesName, string strName) |
156 | var httpCookie = HttpContext.Current.Request.Cookies[cookiesName]; |
157 | if (httpCookie != null && httpCookie[strName] != null ) |
159 | return HttpUtility.UrlDecode(httpCookie.Values[strName]); |
168 | /// <param name="host">域名</param> |
169 | /// <returns></returns> |
170 | public static bool IsValidDomain( string host) |
172 | Regex r = new Regex( @"^\d $" ); |
173 | if (host.IndexOf( "." ) == -1) |
177 | return !r.IsMatch(host.Replace( "." , string .Empty)); |
原文链接:C#操作Cookie方法大全 CookieHelper类库