1 /// <summary> 2 /// Coookie 管理类 3 /// </summary> 4 public class CookieManager 5 { 6 #region 获取 Cookie/Cookie值 7 /// <summary> 8 /// 获得Cookie的值 9 /// </summary> 10 /// <param name="cookieName">Cookie名称</param> 11 /// <returns>返回Cookie值</returns> 12 public static string GetValue(string cookieName) 13 { 14 return GetValue(cookieName, null); 15 } 16 17 /// <summary> 18 /// 获得Cookie子键值 19 /// </summary> 20 /// <param name="cookieName">Cookie名称</param> 21 /// <param name="key">Cookie子键名称</param> 22 /// <returns>返回Cookie子键值</returns> 23 public static string GetValue(string cookieName, string key) 24 { 25 return GetValue(GetCookie(cookieName), key); 26 } 27 28 /// <summary> 29 /// 获得Cookie子键值 30 /// </summary> 31 /// <param name="cookie">Cookie对象</param> 32 /// <param name="key">Cookie子键名称</param> 33 /// <returns>返回Cookie子键值</returns> 34 public static string GetValue(HttpCookie cookie, string key) 35 { 36 if (cookie != null) 37 { 38 if (cookie.HasKeys && !string.IsNullOrEmpty(key)) 39 return cookie.Values[key]; 40 else 41 return cookie.Value; 42 } 43 44 return string.Empty; 45 } 46 47 /// <summary> 48 /// 获取Cookie 49 /// </summary> 50 /// <param name="cookieName">Cookie名称</param> 51 /// <returns>返回HttpCookie对象</returns> 52 public static HttpCookie GetCookie(string cookieName) 53 { 54 HttpRequest request = HttpContext.Current.Request; 55 56 if (request != null && !string.IsNullOrEmpty(cookieName)) 57 return request.Cookies[cookieName]; 58 59 return null; 60 } 61 #endregion 62 63 #region 添加Cookie/带子键Cookie 64 /// <summary> 65 /// 添加Cookie 66 /// </summary> 67 /// <param name="cookieName">Cookie名称</param> 68 /// <param name="value">Cookie值</param> 69 public static void AddCookie(string cookieName, string value) 70 { 71 AddCookie(cookieName, value, null); 72 } 73 74 /// <summary> 75 /// 添加Cookie 76 /// </summary> 77 /// <param name="cookieName">Cookie名称</param> 78 /// <param name="value">Cookie值</param> 79 /// <param name="expires">Cookie到期时间</param> 80 public static void AddCookie(string cookieName, string value, DateTime? expires) 81 { 82 HttpCookie cookie = new HttpCookie(cookieName, value); 83 if (expires.HasValue) 84 { 85 cookie.Expires = expires.Value; 86 } 87 AddCookie(cookie); 88 } 89 90 /// <summary> 91 /// 添加带子键的Cookie(Cooike.Values) 92 /// </summary> 93 /// <param name="cookieName">Cookie名称</param> 94 /// <param name="keys">子键名称</param> 95 /// <param name="values">子键值</param> 96 public static void AddCookie(string cookieName, string[] keys, string[] values) 97 { 98 AddCookie(cookieName, keys, values, null); 99 } 100 101 /// <summary> 102 /// 添加带子键的Cookie(Cooike.Values) 103 /// </summary> 104 /// <param name="cookieName">Cookie名称</param> 105 /// <param name="keys">子键名称</param> 106 /// <param name="values">子键值</param> 107 /// <param name="expires">Cookie到期时间</param> 108 public static void AddCookie(string cookieName, string[] keys, string[] values, DateTime? expires) 109 { 110 if (keys.Length == values.Length) 111 { 112 HttpCookie cookie = new HttpCookie(cookieName); 113 if (expires.HasValue) 114 { 115 cookie.Expires = expires.Value; 116 } 117 for (int i = 0; i < keys.Length; i++) 118 { 119 cookie.Values.Add(keys[i], values[i]); 120 } 121 AddCookie(cookie); 122 } 123 else 124 { 125 throw new Exception("Cookie子键名称的数量与子键值的数量不一致!"); 126 } 127 } 128 129 /// <summary> 130 /// 添加Cookie 131 /// </summary> 132 /// <param name="cookie">Cookie对象</param> 133 public static void AddCookie(HttpCookie cookie) 134 { 135 HttpResponse response = HttpContext.Current.Response; 136 137 if (response != null) 138 { 139 response.Cookies.Add(cookie); 140 } 141 } 142 143 #endregion 144 145 #region 设置/修改Cookie 146 /// <summary> 147 /// 设置Cookie值 148 /// </summary> 149 /// <param name="cookieName">Cookie名称</param> 150 /// <param name="value">Cookie值</param> 151 public static void SetCookie(string cookieName, string value) 152 { 153 SetCookie(cookieName, value, null); 154 } 155 156 /// <summary> 157 /// 设置Cookie值和过期时间 158 /// </summary> 159 /// <param name="cookieName">Cookie名称</param> 160 /// <param name="value">Cookie值</param> 161 /// <param name="expires">Cookie到期时间</param> 162 public static void SetCookie(string cookieName, string value, DateTime? expires) 163 { 164 HttpCookie cookie = GetCookie(cookieName); 165 if (cookie != null) 166 { 167 cookie.Value = value; 168 if (expires.HasValue) 169 { 170 cookie.Expires = expires.Value; 171 } 172 SetCookie(cookie); 173 } 174 } 175 176 /// <summary> 177 /// 设置Cookie过期时间 178 /// </summary> 179 /// <param name="cookieName">Cookie名称</param> 180 /// <param name="expires">Cookie到期时间</param> 181 public static void SetCookie(string cookieName, DateTime expires) 182 { 183 SetCookie(cookieName, null, null, expires); 184 } 185 186 /// <summary> 187 /// 设置/修改Cookie 188 /// </summary> 189 /// <param name="cookieName">Cookie名称</param> 190 /// <param name="keys">子键名称</param> 191 /// <param name="values">子键值</param> 192 public static void SetCookie(string cookieName, string[] keys, string[] values) 193 { 194 SetCookie(cookieName, keys, values, null); 195 } 196 197 /// <summary> 198 /// 设置/修改Cookie 199 /// </summary> 200 /// <param name="cookieName">Cookie名称</param> 201 /// <param name="keys">子键名称</param> 202 /// <param name="values">子键值</param> 203 /// <param name="expires">Cookie到期时间</param> 204 public static void SetCookie(string cookieName, string[] keys, string[] values, DateTime? expires) 205 { 206 HttpCookie cookie = GetCookie(cookieName); 207 if (cookie != null) 208 { 209 if (expires.HasValue) 210 { 211 cookie.Expires = expires.Value; 212 } 213 if (keys != null && values != null) 214 { 215 if (keys.Length == values.Length) 216 { 217 for (int i = 0; i < keys.Length; i++) 218 { 219 if (cookie.HasKeys && !string.IsNullOrEmpty(keys[i])) 220 cookie.Values.Set(keys[i], values[i]); 221 } 222 } 223 else 224 { 225 throw new Exception("Cookie子键名称的数量与子键值的数量不一致!"); 226 } 227 } 228 SetCookie(cookie); 229 } 230 } 231 232 /// <summary> 233 /// 设置/修改Cookie 234 /// </summary> 235 /// <param name="cookie">Cookie对象</param> 236 public static void SetCookie(HttpCookie cookie) 237 { 238 HttpResponse response = HttpContext.Current.Response; 239 240 if (response != null) 241 { 242 response.SetCookie(cookie); 243 } 244 } 245 #endregion 246 247 #region 删除Cookie 248 /// <summary> 249 /// 删除Cookie 250 /// </summary> 251 /// <param name="cookieName">Cookie名称</param> 252 public static void Remove(string cookieName) 253 { 254 HttpCookie cookie = new HttpCookie(cookieName); 255 cookie.Expires = DateTime.Now.AddDays(-1); 256 257 SetCookie(cookie); 258 } 259 260 /// <summary> 261 /// 删除Cookie的子键 262 /// </summary> 263 /// <param name="cookieName">Cookie名称</param> 264 /// <param name="keys">子键名称</param> 265 public static void Remove(string cookieName, string[] keys) 266 { 267 HttpCookie cookie = GetCookie(cookieName); 268 if (cookie != null) 269 { 270 if (cookie.HasKeys && keys != null) 271 { 272 for (int i = 0; i < keys.Length; i++) 273 { 274 cookie.Values.Remove(keys[i]); 275 } 276 } 277 } 278 279 SetCookie(cookie); 280 } 281 #endregion 282 }
时间: 2024-11-25 20:32:49