坐峰怀雪灬

路漫漫其修远兮,吾将上下而求索。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.NET的Cookie相关操作

Posted on 2016-07-15 13:31  坐峰怀雪灬  阅读(207)  评论(0编辑  收藏  举报
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Web;
  5 
  6 namespace Comm
  7 {
  8     public class CookiesHelper
  9     {
 10 
 11         #region 获取Cookie
 12 
 13         /// <summary>
 14         /// 获得Cookie的值
 15         /// </summary>
 16         /// <param name="cookieName"></param>
 17         /// <returns></returns>
 18         public static string GetCookieValue(string cookieName)
 19         {
 20             return GetCookieValue(cookieName, null);
 21         }
 22 
 23         /// <summary>
 24         /// 获得Cookie的值
 25         /// </summary>
 26         /// <param name="cookieName"></param>
 27         /// <param name="key"></param>
 28         /// <returns></returns>
 29         public static string GetCookieValue(string cookieName, string key)
 30         {
 31             HttpRequest request = HttpContext.Current.Request;
 32             if (request != null)
 33                 return GetCookieValue(request.Cookies[cookieName], key);
 34             return "";
 35         }
 36 
 37         /// <summary>
 38         /// 获得Cookie的子键值
 39         /// </summary>
 40         /// <param name="cookie"></param>
 41         /// <param name="key"></param>
 42         /// <returns></returns>
 43         public static string GetCookieValue(HttpCookie cookie, string key)
 44         {
 45             if (cookie != null)
 46             {
 47                 if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
 48                     return cookie.Values[key];
 49                 else
 50                     return cookie.Value;
 51             }
 52             return "";
 53         }
 54 
 55         /// <summary>
 56         /// 获得Cookie
 57         /// </summary>
 58         /// <param name="cookieName"></param>
 59         /// <returns></returns>
 60         public static HttpCookie GetCookie(string cookieName)
 61         {
 62             HttpRequest request = HttpContext.Current.Request;
 63             if (request != null)
 64                 return request.Cookies[cookieName];
 65             return null;
 66         }
 67 
 68         #endregion
 69 
 70         #region 删除Cookie
 71 
 72         /// <summary>
 73         /// 删除Cookie
 74         /// </summary>
 75         /// <param name="cookieName"></param>
 76         public static void RemoveCookie(string cookieName)
 77         {
 78             RemoveCookie(cookieName, null);
 79         }
 80 
 81         /// <summary>
 82         /// 删除Cookie的子键
 83         /// </summary>
 84         /// <param name="cookieName"></param>
 85         /// <param name="key"></param>
 86         public static void RemoveCookie(string cookieName, string key)
 87         {
 88             HttpResponse response = HttpContext.Current.Response;
 89             if (response != null)
 90             {
 91                 HttpCookie cookie = response.Cookies[cookieName];
 92                 if (cookie != null)
 93                 {
 94                     if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
 95                         cookie.Values.Remove(key);
 96                     else
 97                         response.Cookies.Remove(cookieName);
 98                 }
 99             }
100         }
101 
102         #endregion
103 
104         #region 设置/修改Cookie
105 
106         /// <summary>
107         /// 设置Cookie子键的值
108         /// </summary>
109         /// <param name="cookieName"></param>
110         /// <param name="key"></param>
111         /// <param name="value"></param>
112         public static void SetCookie(string cookieName, string key, string value)
113         {
114             SetCookie(cookieName, key, value, null);
115         }
116 
117         /// <summary>
118         /// 设置Cookie值
119         /// </summary>
120         /// <param name="key"></param>
121         /// <param name="value"></param>
122         public static void SetCookie(string key, string value)
123         {
124             SetCookie(key, null, value, null);
125         }
126 
127         /// <summary>
128         /// 设置Cookie值和过期时间
129         /// </summary>
130         /// <param name="key"></param>
131         /// <param name="value"></param>
132         /// <param name="expires"></param>
133         public static void SetCookie(string key, string value, DateTime expires)
134         {
135             SetCookie(key, null, value, expires);
136         }
137 
138         /// <summary>
139         /// 设置Cookie过期时间
140         /// </summary>
141         /// <param name="cookieName"></param>
142         /// <param name="expires"></param>
143         public static void SetCookie(string cookieName, DateTime expires)
144         {
145             SetCookie(cookieName, null, null, expires);
146         }
147 
148         /// <summary>
149         /// 设置Cookie
150         /// </summary>
151         /// <param name="cookieName"></param>
152         /// <param name="key"></param>
153         /// <param name="value"></param>
154         /// <param name="expires"></param>
155         public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
156         {
157             HttpResponse response = HttpContext.Current.Response;
158             if (response != null)
159             {
160                 HttpCookie cookie = response.Cookies[cookieName];
161                 if (cookie != null)
162                 {
163                     if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
164                         cookie.Values.Set(key, value);
165                     else
166                         if (!string.IsNullOrEmpty(value))
167                             cookie.Value = value;
168                     if (expires != null)
169                         cookie.Expires = expires.Value;
170                     response.SetCookie(cookie);
171                 }
172             }
173 
174         }
175 
176         #endregion
177 
178         #region 添加Cookie
179 
180         /// <summary>
181         /// 添加Cookie
182         /// </summary>
183         /// <param name="key"></param>
184         /// <param name="value"></param>
185         public static void AddCookie(string key, string value)
186         {
187             AddCookie(new HttpCookie(key, value));
188         }
189 
190         /// <summary>
191         /// 添加Cookie
192         /// </summary>
193         /// <param name="key"></param>
194         /// <param name="value"></param>
195         /// <param name="expires"></param>
196         public static void AddCookie(string key, string value, DateTime expires)
197         {
198             HttpCookie cookie = new HttpCookie(key, value);
199             cookie.Expires = expires;
200             AddCookie(cookie);
201         }
202 
203         /// <summary>
204         /// 添加为Cookie.Values集合
205         /// </summary>
206         /// <param name="cookieName"></param>
207         /// <param name="key"></param>
208         /// <param name="value"></param>
209         public static void AddCookie(string cookieName, string key, string value)
210         {
211             HttpCookie cookie = new HttpCookie(cookieName);
212             cookie.Values.Add(key, value);
213             AddCookie(cookie);
214         }
215 
216         /// <summary>
217         /// 添加为Cookie集合
218         /// </summary>
219         /// <param name="cookieName">Cookie名称</param>
220         /// <param name="expires">过期时间</param>
221         public static void AddCookie(string cookieName, DateTime expires)
222         {
223             HttpCookie cookie = new HttpCookie(cookieName);
224             cookie.Expires = expires;
225             AddCookie(cookie);
226         }
227 
228         /// <summary>
229         /// 添加为Cookie.Values集合
230         /// </summary>
231         /// <param name="cookieName"></param>
232         /// <param name="key"></param>
233         /// <param name="value"></param>
234         /// <param name="expires"></param>
235         public static void AddCookie(string cookieName, string key, string value, DateTime expires)
236         {
237             HttpCookie cookie = new HttpCookie(cookieName);
238             cookie.Expires = expires;
239             cookie.Values.Add(key, value);
240             AddCookie(cookie);
241         }
242 
243         /// <summary>
244         /// 添加Cookie
245         /// </summary>
246         /// <param name="cookie"></param>
247         public static void AddCookie(HttpCookie cookie)
248         {
249             HttpResponse response = HttpContext.Current.Response;
250             if (response != null)
251             {
252                 //指定客户端脚本是否可以访问[默认为false]
253                 cookie.HttpOnly = true;
254                 //指定统一的Path,比便能通存通取
255                 cookie.Path = "/";
256                 //设置跨域,这样在其它二级域名下就都可以访问到了
257                 //cookie.Domain = "chinesecoo.com";
258                 response.AppendCookie(cookie);
259             }
260         }
261 
262         #endregion
263     }
264 }