1.方案1
1 public static class Cookie 2 { 3 /// 创建COOKIE对象并赋Value值 4 /// <summary> 5 /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires 6 /// </summary> 7 /// <param name="strCookieName">COOKIE对象名</param> 8 /// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param> 9 /// <param name="strValue">COOKIE对象Value值</param> 10 public static void SetObj(string strCookieName, int iExpires, string strValue) 11 { 12 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 13 objCookie.Value = Utils.EncodeURIComponent(strValue.Trim()); 14 objCookie.Domain = N0.Config.CommonConfig.strDomain; 15 if (iExpires > 0) 16 { 17 if (iExpires == 1) 18 { 19 objCookie.Expires = DateTime.MaxValue; 20 } 21 else 22 { 23 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 24 } 25 } 26 HttpContext.Current.Response.Cookies.Add(objCookie); 27 } 28 /// 创建COOKIE对象并赋多个KEY键值 29 /// <summary> 30 /// 创建COOKIE对象并赋多个KEY键值 31 /// 设键/值如下: 32 /// NameValueCollection myCol = new NameValueCollection(); 33 /// myCol.Add("red", "rojo"); 34 /// myCol.Add("green", "verde"); 35 /// myCol.Add("blue", "azul"); 36 /// myCol.Add("red", "rouge"); 结果“red:rojo,rouge;green:verde;blue:azul” 37 /// </summary> 38 /// <param name="strCookieName">COOKIE对象名</param> 39 /// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param> 40 /// <param name="KeyValue">键/值对集合</param> 41 public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue) 42 { 43 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 44 foreach (String key in KeyValue.AllKeys) 45 { 46 objCookie[key] = Utils.EncodeURIComponent(KeyValue[key].Trim()); 47 } 48 objCookie.Domain = N0.Config.CommonConfig.strDomain; 49 if (iExpires > 0) 50 { 51 if (iExpires == 1) 52 { 53 objCookie.Expires = DateTime.MaxValue; 54 } 55 else 56 { 57 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 58 } 59 } 60 HttpContext.Current.Response.Cookies.Add(objCookie); 61 } 62 /// 创建COOKIE对象并赋Value值 63 /// <summary> 64 /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires 65 /// </summary> 66 /// <param name="strCookieName">COOKIE对象名</param> 67 /// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param> 68 /// <param name="strDomain">作用域</param> 69 /// <param name="strValue">COOKIE对象Value值</param> 70 public static void SetObj(string strCookieName, int iExpires, string strValue, string strDomain) 71 { 72 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 73 objCookie.Value = Utils.EncodeURIComponent(strValue.Trim()); 74 objCookie.Domain = strDomain.Trim(); 75 if (iExpires > 0) 76 { 77 if (iExpires == 1) 78 { 79 objCookie.Expires = DateTime.MaxValue; 80 } 81 else 82 { 83 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 84 } 85 } 86 HttpContext.Current.Response.Cookies.Add(objCookie); 87 } 88 /// 创建COOKIE对象并赋多个KEY键值 89 /// <summary> 90 /// 创建COOKIE对象并赋多个KEY键值 91 /// 设键/值如下: 92 /// NameValueCollection myCol = new NameValueCollection(); 93 /// myCol.Add("red", "rojo"); 94 /// myCol.Add("green", "verde"); 95 /// myCol.Add("blue", "azul"); 96 /// myCol.Add("red", "rouge"); 结果“red:rojo,rouge;green:verde;blue:azul” 97 /// </summary> 98 /// <param name="strCookieName">COOKIE对象名</param> 99 /// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param> 100 /// <param name="strDomain">作用域</param> 101 /// <param name="KeyValue">键/值对集合</param> 102 public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomain) 103 { 104 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 105 foreach (String key in KeyValue.AllKeys) 106 { 107 objCookie[key] = Utils.EncodeURIComponent(KeyValue[key].Trim()); 108 } 109 objCookie.Domain = strDomain.Trim(); 110 if (iExpires > 0) 111 { 112 if (iExpires == 1) 113 { 114 objCookie.Expires = DateTime.MaxValue; 115 } 116 else 117 { 118 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 119 } 120 } 121 HttpContext.Current.Response.Cookies.Add(objCookie); 122 } 123 124 /// 读取Cookie某个对象的Value值 125 /// <summary> 126 /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回字符串"CookieNonexistence" 127 /// </summary> 128 /// <param name="strCookieName">Cookie对象名称</param> 129 /// <returns>Value值,如果对象本就不存在,则返回字符串"CookieNonexistence"</returns> 130 public static string GetValue(string strCookieName) 131 { 132 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 133 { 134 return "CookieNonexistence"; 135 } 136 else 137 { 138 return Utils.DecodeURIComponent(HttpContext.Current.Request.Cookies[strCookieName].Value); 139 } 140 } 141 /// 读取Cookie某个对象的某个Key键的键值 142 /// <summary> 143 /// 读取Cookie某个对象的某个Key键的键值,返回Key键值,如果对象本就不存在,则返回字符串"CookieNonexistence",如果Key键不存在,则返回字符串"KeyNonexistence" 144 /// </summary> 145 /// <param name="strCookieName">Cookie对象名称</param> 146 /// <param name="strKeyName">Key键名</param> 147 /// <returns>Key键值,如果对象本就不存在,则返回字符串"CookieNonexistence",如果Key键不存在,则返回字符串"KeyNonexistence"</returns> 148 public static string GetValue(string strCookieName,string strKeyName) 149 { 150 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 151 { 152 return "CookieNonexistence"; 153 } 154 else 155 { 156 string strObjValue = Utils.DecodeURIComponent(HttpContext.Current.Request.Cookies[strCookieName].Value); 157 string strKeyName2 = strKeyName+"="; 158 if (strObjValue.IndexOf(strKeyName2) == -1) 159 { 160 return "KeyNonexistence"; 161 } 162 else 163 { 164 return Utils.DecodeURIComponent(HttpContext.Current.Request.Cookies[strCookieName][strKeyName]); 165 } 166 } 167 } 168 169 /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法 170 /// <summary> 171 /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法,操作成功返回字符串"success",如果对象本就不存在,则返回字符串"CookieNonexistence"。 172 /// </summary> 173 /// <param name="strCookieName">Cookie对象名称</param> 174 /// <param name="strKeyName">Key键名</param> 175 /// <param name="KeyValue">Key键值</param> 176 /// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365)。注意:虽是修改功能,实则重建覆盖,所以时间也要重设,因为没办法获得旧的有效期</param> 177 /// <returns>如果对象本就不存在,则返回字符串"CookieNonexistence",如果操作成功返回字符串"success"。</returns> 178 public static string Edit(string strCookieName, string strKeyName, string KeyValue,int iExpires) 179 { 180 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 181 { 182 return "CookieNonexistence"; 183 } 184 else 185 { 186 HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName]; 187 objCookie[strKeyName] = Utils.EncodeURIComponent(KeyValue.Trim()); 188 objCookie.Domain = N0.Config.CommonConfig.strDomain; 189 if (iExpires > 0) 190 { 191 if (iExpires == 1) 192 { 193 objCookie.Expires = DateTime.MaxValue; 194 } 195 else 196 { 197 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 198 } 199 } 200 HttpContext.Current.Response.Cookies.Add(objCookie); 201 return "success"; 202 } 203 } 204 205 /// 删除COOKIE对象 206 /// <summary> 207 /// 删除COOKIE对象 208 /// </summary> 209 /// <param name="strCookieName">Cookie对象名称</param> 210 public static void Del(string strCookieName) 211 { 212 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 213 objCookie.Domain = N0.Config.CommonConfig.strDomain; 214 objCookie.Expires = DateTime.Now.AddYears(-5); 215 HttpContext.Current.Response.Cookies.Add(objCookie); 216 } 217 /// 删除某个COOKIE对象某个Key子键 218 /// <summary> 219 /// 删除某个COOKIE对象某个Key子键,操作成功返回字符串"success",如果对象本就不存在,则返回字符串"CookieNonexistence" 220 /// </summary> 221 /// <param name="strCookieName">Cookie对象名称</param> 222 /// <param name="strKeyName">Key键名</param> 223 /// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365)。注意:虽是修改功能,实则重建覆盖,所以时间也要重设,因为没办法获得旧的有效期</param> 224 /// <returns>如果对象本就不存在,则返回字符串"CookieNonexistence",如果操作成功返回字符串"success"。</returns> 225 public static string Del(string strCookieName, string strKeyName, int iExpires) 226 { 227 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 228 { 229 return "CookieNonexistence"; 230 } 231 else 232 { 233 HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName]; 234 objCookie.Values.Remove(strKeyName); 235 objCookie.Domain = N0.Config.CommonConfig.strDomain; 236 if (iExpires > 0) 237 { 238 if (iExpires == 1) 239 { 240 objCookie.Expires = DateTime.MaxValue; 241 } 242 else 243 { 244 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 245 } 246 } 247 HttpContext.Current.Response.Cookies.Add(objCookie); 248 return "success"; 249 } 250 } 251 } 252 /// Session操作类 253 /// <summary> 254 /// Session操作类 255 /// </summary> 256 public static class SessionCustom 257 { 258 /// 添加Session,调动有效期默认为23分钟 259 /// <summary> 260 /// 添加Session,调动有效期默认为23分钟 261 /// </summary> 262 /// <param name="strSessionName">Session对象名称</param> 263 /// <param name="strValue">Session值</param> 264 public static void Add(string strSessionName, string strValue) 265 { 266 HttpContext.Current.Session[strSessionName] = strValue; 267 HttpContext.Current.Session.Timeout = 23; 268 } 269 /// 添加Session 270 /// <summary> 271 /// 添加Session 272 /// </summary> 273 /// <param name="strSessionName">Session对象名称</param> 274 /// <param name="strValue">Session值</param> 275 /// <param name="iExpires">调动有效期(分钟)</param> 276 public static void Add(string strSessionName, string strValue, int iExpires) 277 { 278 HttpContext.Current.Session[strSessionName] = strValue; 279 HttpContext.Current.Session.Timeout = iExpires; 280 } 281 282 /// 读取某个Session对象值 283 /// <summary> 284 /// 读取某个Session对象值 285 /// </summary> 286 /// <param name="strSessionName">Session对象名称</param> 287 /// <returns>Session对象值</returns> 288 public static string Get(string strSessionName) 289 { 290 if (HttpContext.Current.Session[strSessionName] == null) 291 { 292 return null; 293 } 294 else 295 { 296 return HttpContext.Current.Session[strSessionName].ToString(); 297 } 298 } 299 300 /// 删除某个Session对象 301 /// <summary> 302 /// 删除某个Session对象 303 /// </summary> 304 /// <param name="strSessionName">Session对象名称</param> 305 public static void Del(string strSessionName) 306 { 307 HttpContext.Current.Session[strSessionName] = null; 308 } 309 } 310 311 /// Cache操作类 312 /// <summary> 313 /// Cache操作类 314 /// </summary> 315 public static class CacheCustom 316 { 317 /// 简单创建/修改Cache,前提是这个值是字符串形式的 318 /// <summary> 319 /// 简单创建/修改Cache,前提是这个值是字符串形式的 320 /// </summary> 321 /// <param name="strCacheName">Cache名称</param> 322 /// <param name="strValue">Cache值</param> 323 /// <param name="iExpires">有效期,秒数(使用的是当前时间+秒数得到一个绝对到期值)</param> 324 /// <param name="priority">保留优先级,1最不会被清除,6最容易被内存管理清除(1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low)</param> 325 public static void Insert(string strCacheName, string strValue, int iExpires, int priority) 326 { 327 TimeSpan ts = new TimeSpan(0, 0, iExpires); 328 CacheItemPriority cachePriority; 329 switch (priority) 330 { 331 case 6: 332 cachePriority = CacheItemPriority.Low; 333 break; 334 case 5: 335 cachePriority = CacheItemPriority.BelowNormal; 336 break; 337 case 4: 338 cachePriority = CacheItemPriority.Normal; 339 break; 340 case 3: 341 cachePriority = CacheItemPriority.AboveNormal; 342 break; 343 case 2: 344 cachePriority = CacheItemPriority.High; 345 break; 346 case 1: 347 cachePriority = CacheItemPriority.NotRemovable; 348 break; 349 default: 350 cachePriority = CacheItemPriority.Default; 351 break; 352 } 353 HttpContext.Current.Cache.Insert(strCacheName, strValue, null, DateTime.Now.Add(ts), System.Web.Caching.Cache.NoSlidingExpiration, cachePriority, null); 354 } 355 356 /// 简单读书Cache对象的值,前提是这个值是字符串形式的 357 /// <summary> 358 /// 简单读书Cache对象的值,前提是这个值是字符串形式的 359 /// </summary> 360 /// <param name="strCacheName">Cache名称</param> 361 /// <returns>Cache字符串值</returns> 362 public static string Get(string strCacheName) 363 { 364 return HttpContext.Current.Cache[strCacheName].ToString(); 365 } 366 367 /// 删除Cache对象 368 /// <summary> 369 /// 删除Cache对象 370 /// </summary> 371 /// <param name="strCacheName">Cache名称</param> 372 public static void Del(string strCacheName) 373 { 374 HttpContext.Current.Cache.Remove(strCacheName); 375 } 376 }
2.方案2
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Collections.Specialized; 7 8 namespace Core.Common.Web 9 { 10 /// <summary> 11 /// Cookie静态操作类 12 /// </summary> 13 public static class Cookie 14 { 15 #region 静态方法 16 17 /// <summary> 18 /// 创建或修改COOKIE对象并赋Value值 19 /// </summary> 20 /// <param name="strCookieName">COOKIE对象名</param> 21 /// <param name="strValue">COOKIE对象Value值</param> 22 /// <remarks> 23 /// 对COOKIE修改必须重新设Expires 24 /// </remarks> 25 public static void SetObject(string strCookieName, string strValue) 26 { 27 SetObject(strCookieName, 1, strValue); 28 } 29 30 /// <summary> 31 /// 创建或修改COOKIE对象并赋Value值 32 /// </summary> 33 /// <param name="strCookieName">COOKIE对象名</param> 34 /// <param name="iExpires"> 35 /// COOKIE对象有效时间(秒数) 36 /// 1表示永久有效,0和负数都表示不设有效时间 37 /// 大于等于2表示具体有效秒数 38 /// 86400秒 = 1天 = (60*60*24) 39 /// 604800秒 = 1周 = (60*60*24*7) 40 /// 2593000秒 = 1月 = (60*60*24*30) 41 /// 31536000秒 = 1年 = (60*60*24*365) 42 /// </param> 43 /// <param name="strValue">COOKIE对象Value值</param> 44 /// <remarks> 45 /// 对COOKIE修改必须重新设Expires 46 /// </remarks> 47 public static void SetObject(string strCookieName, int iExpires, string strValue) 48 { 49 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 50 objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim()); 51 if (iExpires > 0) 52 { 53 if (iExpires == 1) 54 { 55 objCookie.Expires = DateTime.MaxValue; 56 } 57 else 58 { 59 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 60 } 61 } 62 HttpContext.Current.Response.Cookies.Add(objCookie); 63 } 64 65 /// <summary> 66 /// 创建COOKIE对象并赋多个KEY键值 67 /// </summary> 68 /// <param name="strCookieName">COOKIE对象名</param> 69 /// <param name="iExpires"> 70 /// COOKIE对象有效时间(秒数) 71 /// 1表示永久有效,0和负数都表示不设有效时间 72 /// 大于等于2表示具体有效秒数 73 /// 86400秒 = 1天 = (60*60*24) 74 /// 604800秒 = 1周 = (60*60*24*7) 75 /// 2593000秒 = 1月 = (60*60*24*30) 76 /// 31536000秒 = 1年 = (60*60*24*365) 77 /// </param> 78 /// <param name="KeyValue">键/值对集合</param> 79 public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue) 80 { 81 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 82 foreach (string key in KeyValue.AllKeys) 83 { 84 objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim()); 85 } 86 if (iExpires > 0) 87 { 88 if (iExpires == 1) 89 { 90 objCookie.Expires = DateTime.MaxValue; 91 } 92 else 93 { 94 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 95 } 96 } 97 HttpContext.Current.Response.Cookies.Add(objCookie); 98 } 99 100 /// <summary> 101 /// 创建或修改COOKIE对象并赋Value值 102 /// </summary> 103 /// <param name="strCookieName">COOKIE对象名</param> 104 /// <param name="iExpires"> 105 /// COOKIE对象有效时间(秒数) 106 /// 1表示永久有效,0和负数都表示不设有效时间 107 /// 大于等于2表示具体有效秒数 108 /// 86400秒 = 1天 = (60*60*24) 109 /// 604800秒 = 1周 = (60*60*24*7) 110 /// 2593000秒 = 1月 = (60*60*24*30) 111 /// 31536000秒 = 1年 = (60*60*24*365) 112 /// </param> 113 /// <param name="strDomain">作用域</param> 114 /// <param name="strValue">COOKIE对象Value值</param> 115 /// <remarks> 116 /// 对COOKIE修改必须重新设Expires 117 /// </remarks> 118 public static void SetObject(string strCookieName, int iExpires, string strValue, string strDomain) 119 { 120 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 121 objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim()); 122 objCookie.Domain = strDomain.Trim(); 123 if (iExpires > 0) 124 { 125 if (iExpires == 1) 126 { 127 objCookie.Expires = DateTime.MaxValue; 128 } 129 else 130 { 131 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 132 } 133 } 134 HttpContext.Current.Response.Cookies.Add(objCookie); 135 } 136 137 /// <summary> 138 /// 创建COOKIE对象并赋多个KEY键值 139 /// </summary> 140 /// <param name="strCookieName">COOKIE对象名</param> 141 /// <param name="iExpires"> 142 /// COOKIE对象有效时间(秒数) 143 /// 1表示永久有效,0和负数都表示不设有效时间 144 /// 大于等于2表示具体有效秒数 145 /// 86400秒 = 1天 = (60*60*24) 146 /// 604800秒 = 1周 = (60*60*24*7) 147 /// 2593000秒 = 1月 = (60*60*24*30) 148 /// 31536000秒 = 1年 = (60*60*24*365) 149 /// </param> 150 /// <param name="strDomain">作用域</param> 151 /// <param name="KeyValue">键/值对集合</param> 152 public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomain) 153 { 154 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 155 foreach (string key in KeyValue.AllKeys) 156 { 157 objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim()); 158 } 159 objCookie.Domain = strDomain.Trim(); 160 if (iExpires > 0) 161 { 162 if (iExpires == 1) 163 { 164 objCookie.Expires = DateTime.MaxValue; 165 } 166 else 167 { 168 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 169 } 170 } 171 HttpContext.Current.Response.Cookies.Add(objCookie); 172 } 173 174 /// <summary> 175 /// 读取Cookie某个对象的Value值,返回Value值 176 /// </summary> 177 /// <param name="strCookieName">Cookie对象名称</param> 178 /// <returns>Value值,如果对象本就不存在,则返回 null</returns> 179 public static string GetValue(string strCookieName) 180 { 181 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 182 { 183 return null; 184 } 185 else 186 { 187 return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName].Value); 188 } 189 } 190 191 /// <summary> 192 /// 读取Cookie某个对象的某个Key键的键值 193 /// </summary> 194 /// <param name="strCookieName">Cookie对象名称</param> 195 /// <param name="strKeyName">Key键名</param> 196 /// <returns>Key键值,如果对象或键值就不存在,则返回 null</returns> 197 public static string GetValue(string strCookieName, string strKeyName) 198 { 199 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 200 { 201 return null; 202 } 203 else 204 { 205 string strObjValue = HttpContext.Current.Request.Cookies[strCookieName].Value; 206 string strKeyName2 = strKeyName + "="; 207 if (strObjValue.IndexOf(strKeyName2) == -1) 208 { 209 return null; 210 } 211 else 212 { 213 return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName][strKeyName]); 214 } 215 } 216 } 217 218 /// <summary> 219 /// 修改某个COOKIE对象某个Key键的键值或给某个COOKIE对象添加Key键 220 /// </summary> 221 /// <param name="strCookieName">Cookie对象名称</param> 222 /// <param name="strKeyName">Key键名</param> 223 /// <param name="KeyValue">Key键值</param> 224 /// <param name="iExpires"> 225 /// COOKIE对象有效时间(秒数) 226 /// 1表示永久有效,0和负数都表示不设有效时间 227 /// 大于等于2表示具体有效秒数 228 /// 86400秒 = 1天 = (60*60*24) 229 /// 604800秒 = 1周 = (60*60*24*7) 230 /// 2593000秒 = 1月 = (60*60*24*30) 231 /// 31536000秒 = 1年 = (60*60*24*365) 232 /// </param> 233 /// <returns>如果对象本就不存在,则返回 false</returns> 234 public static bool Edit(string strCookieName, string strKeyName, string KeyValue, int iExpires) 235 { 236 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 237 { 238 return false; 239 } 240 else 241 { 242 HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName]; 243 objCookie[strKeyName] = HttpContext.Current.Server.UrlEncode(KeyValue.Trim()); 244 if (iExpires > 0) 245 { 246 if (iExpires == 1) 247 { 248 objCookie.Expires = DateTime.MaxValue; 249 } 250 else 251 { 252 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 253 } 254 } 255 HttpContext.Current.Response.Cookies.Add(objCookie); 256 return true; 257 } 258 } 259 260 /// <summary> 261 /// 删除COOKIE对象 262 /// </summary> 263 /// <param name="strCookieName">Cookie对象名称</param> 264 public static void Delete(string strCookieName) 265 { 266 HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); 267 objCookie.Expires = DateTime.Now.AddYears(-5); 268 HttpContext.Current.Response.Cookies.Add(objCookie); 269 } 270 271 /// <summary> 272 /// 删除某个COOKIE对象某个Key子键 273 /// </summary> 274 /// <param name="strCookieName">Cookie对象名称</param> 275 /// <param name="strKeyName">Key键名</param> 276 /// <param name="iExpires"> 277 /// COOKIE对象有效时间(秒数) 278 /// 1表示永久有效,0和负数都表示不设有效时间 279 /// 大于等于2表示具体有效秒数 280 /// 86400秒 = 1天 = (60*60*24) 281 /// 604800秒 = 1周 = (60*60*24*7) 282 /// 2593000秒 = 1月 = (60*60*24*30) 283 /// 31536000秒 = 1年 = (60*60*24*365) 284 /// </param> 285 /// <returns>如果对象本就不存在,则返回 false</returns> 286 public static bool Delete(string strCookieName, string strKeyName, int iExpires) 287 { 288 if (HttpContext.Current.Request.Cookies[strCookieName] == null) 289 { 290 return false; 291 } 292 else 293 { 294 HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName]; 295 objCookie.Values.Remove(strKeyName); 296 if (iExpires > 0) 297 { 298 if (iExpires == 1) 299 { 300 objCookie.Expires = DateTime.MaxValue; 301 } 302 else 303 { 304 objCookie.Expires = DateTime.Now.AddSeconds(iExpires); 305 } 306 } 307 HttpContext.Current.Response.Cookies.Add(objCookie); 308 return true; 309 } 310 } 311 312 #endregion 313 } 314 }