现在的位置: 首页 > 综合 > 正文

转:ASP.NET完美操作cookies

2012年12月27日 ⁄ 综合 ⁄ 共 8640字 ⁄ 字号 评论关闭

刚到博客园,先转一些贴与大家分享一下。也可以讨论一下。

  1. using System; 
  2.  using System.Web; 
  3.   
  4.  namespace Moosoft.OA.Public 
  5.  { 
  6.      /// <summary> 
  7.      /// Cookie帮助类 
  8.      /// </summary> 
  9.      public class CookiesHelper 

10.       { 

11.    

12.           #region 获取Cookie 

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.               } 

  1.          } 
  2.   
  3.          #endregion 
  4.   
  5.          #region 设置/修改Cookie 
  6.   
  7.          /// <summary> 
  8.          /// 设置Cookie子键的值 
  9.          /// </summary> 
  10.          /// <param name="cookieName"></param> 
  11.          /// <param name="key"></param> 
  12.          /// <param name="value"></param> 
  13.          public static void SetCookie(string cookieName, string key, string value) 
  14.          { 
  15.              SetCookie(cookieName, key, value, null); 
  16.          } 
  17.   
  18.          /// <summary> 
  19.          /// 设置Cookie值 
  20.          /// </summary> 
  21.          /// <param name="key"></param> 
  22.          /// <param name="value"></param> 
  23.          public static void SetCookie(string key, string value) 
  24.          { 
  25.              SetCookie(key, null, value, null); 
  26.          } 
  27.   
  28.          /// <summary> 
  29.          /// 设置Cookie值和过期时间 
  30.          /// </summary> 
  31.          /// <param name="key"></param> 
  32.          /// <param name="value"></param> 
  33.          /// <param name="expires"></param> 
  34.          public static void SetCookie(string key, string value, DateTime expires) 
  35.          { 
  36.              SetCookie(key, null, value, expires); 
  37.          } 
  38.   
  39.          /// <summary> 
  40.          /// 设置Cookie过期时间 
  41.          /// </summary> 
  42.          /// <param name="cookieName"></param> 
  43.          /// <param name="expires"></param> 
  44.          public static void SetCookie(string cookieName, DateTime expires) 
  45.          { 
  46.              SetCookie(cookieName, nullnull, expires); 
  47.          } 
  48.   
  49.          /// <summary> 
  50.          /// 设置Cookie 
  51.          /// </summary> 
  52.          /// <param name="cookieName"></param> 
  53.          /// <param name="key"></param> 
  54.          /// <param name="value"></param> 
  55.          /// <param name="expires"></param> 
  56.          public static void SetCookie(string cookieName, string key, string value, DateTime? expires) 
  57.          { 
  58.              HttpResponse response = HttpContext.Current.Response; 
  59.              if (response != null
  60.              { 
  61.                  HttpCookie cookie = response.Cookies[cookieName]; 
  62.                  if (cookie != null
  63.                  { 
  64.                      if (!string.IsNullOrEmpty(key) && cookie.HasKeys) 
  65.                          cookie.Values.Set(key, value); 
  66.                      else 
  67.                          if (!string.IsNullOrEmpty(value)) 
  68.                              cookie.Value = value; 
  69.                      if (expires != null
  70.                          cookie.Expires = expires.Value; 
  71.                      response.SetCookie(cookie); 
  72.                  } 
  73.              } 
  74.   
  75.          } 
  76.   
  77.          #endregion 
  78.   
  79.          #region 添加Cookie 
  80.   
  81.          /// <summary> 
  82.          /// 添加Cookie 
  83.          /// </summary> 
  84.          /// <param name="key"></param> 
  85.          /// <param name="value"></param> 
  86.          public static void AddCookie(string key, string value) 
  87.          { 
  88.              AddCookie(new HttpCookie(key, value)); 
  89.          } 
  90.   
  91.          /// <summary> 
  92.          /// 添加Cookie 
  93.          /// </summary> 
  94.          /// <param name="key"></param> 
  95.          /// <param name="value"></param> 
  96.          /// <param name="expires"></param> 
  97.          public static void AddCookie(string key, string value, DateTime expires) 
  98.          { 
  99.              HttpCookie cookie = new HttpCookie(key, value); 
  100.              cookie.Expires = expires; 
  101.              AddCookie(cookie); 
  102.          } 
  103.   
  104.          /// <summary> 
  105.          /// 添加为Cookie.Values集合 
  106.          /// </summary> 
  107.          /// <param name="cookieName"></param> 
  108.          /// <param name="key"></param> 
  109.          /// <param name="value"></param> 
  110.          public static void AddCookie(string cookieName, string key, string value) 
  111.          { 
  112.              HttpCookie cookie = new HttpCookie(cookieName); 
  113.              cookie.Values.Add(key, value); 
  114.              AddCookie(cookie); 
  115.          } 
  116.   
  117.          /// <summary> 
  118.          /// 添加为Cookie集合 
  119.          /// </summary> 
  120.          /// <param name="cookieName">Cookie名称</param> 
  121.          /// <param name="expires">过期时间</param> 
  122.          public static void AddCookie(string cookieName, DateTime expires) 
  123.          { 
  124.              HttpCookie cookie = new HttpCookie(cookieName); 
  125.             cookie.Expires = expires; 
  126.              AddCookie(cookie); 
  127.          } 
  128.   
  129.          /// <summary> 
  130.          /// 添加为Cookie.Values集合 
  131.          /// </summary> 
  132.          /// <param name="cookieName"></param> 
  133.          /// <param name="key"></param> 
  134.          /// <param name="value"></param> 
  135.          /// <param name="expires"></param> 
  136.          public static void AddCookie(string cookieName, string key, string value, DateTime expires) 
  137.          { 
  138.              HttpCookie cookie = new HttpCookie(cookieName); 
  139.              cookie.Expires = expires; 
  140.              cookie.Values.Add(key, value); 
  141.              AddCookie(cookie); 
  142.          } 
  143.   
  144.          /// <summary> 
  145.          /// 添加Cookie 
  146.          /// </summary> 
  147.          /// <param name="cookie"></param> 
  148.          public static void AddCookie(HttpCookie cookie) 
  149.          { 
  150.              HttpResponse response = HttpContext.Current.Response; 
  151.              if (response != null
  152.              { 
  153.                  //指定客户端脚本是否可以访问[默认为false] 
  154.                  cookie.HttpOnly = true
  155.                  //指定统一的Path,比便能通存通取 
  156.                  cookie.Path = "/"; 
  157.                  //设置跨域,这样在其它二级域名下就都可以访问到了 
  158.                  //cookie.Domain = "chinesecoo.com"; 
  159.                  response.AppendCookie(cookie); 
  160.              } 
  161.          } 
  162.   
  163.          #endregion 
  164.      } 
  165.  } 

 

抱歉!评论已关闭.