刚到博客园,先转一些贴与大家分享一下。也可以讨论一下。
-
using System;
- using System.Web;
-
- namespace Moosoft.OA.Public
- {
- /// <summary>
- /// Cookie帮助类
- /// </summary>
- 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. }
- }
-
- #endregion
-
- #region 设置/修改Cookie
-
- /// <summary>
- /// 设置Cookie子键的值
- /// </summary>
- /// <param name="cookieName"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void SetCookie(string cookieName, string key, string value)
- {
- SetCookie(cookieName, key, value, null);
- }
-
- /// <summary>
- /// 设置Cookie值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void SetCookie(string key, string value)
- {
- SetCookie(key, null, value, null);
- }
-
- /// <summary>
- /// 设置Cookie值和过期时间
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="expires"></param>
- public static void SetCookie(string key, string value, DateTime expires)
- {
- SetCookie(key, null, value, expires);
- }
-
- /// <summary>
- /// 设置Cookie过期时间
- /// </summary>
- /// <param name="cookieName"></param>
- /// <param name="expires"></param>
- public static void SetCookie(string cookieName, DateTime expires)
- {
- SetCookie(cookieName, null, null, expires);
- }
-
- /// <summary>
- /// 设置Cookie
- /// </summary>
- /// <param name="cookieName"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="expires"></param>
- public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
- {
- HttpResponse response = HttpContext.Current.Response;
- if (response != null)
- {
- HttpCookie cookie = response.Cookies[cookieName];
- if (cookie != null)
- {
- if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
- cookie.Values.Set(key, value);
- else
- if (!string.IsNullOrEmpty(value))
- cookie.Value = value;
- if (expires != null)
- cookie.Expires = expires.Value;
- response.SetCookie(cookie);
- }
- }
-
- }
-
- #endregion
-
- #region 添加Cookie
-
- /// <summary>
- /// 添加Cookie
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void AddCookie(string key, string value)
- {
- AddCookie(new HttpCookie(key, value));
- }
-
- /// <summary>
- /// 添加Cookie
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="expires"></param>
- public static void AddCookie(string key, string value, DateTime expires)
- {
- HttpCookie cookie = new HttpCookie(key, value);
- cookie.Expires = expires;
- AddCookie(cookie);
- }
-
- /// <summary>
- /// 添加为Cookie.Values集合
- /// </summary>
- /// <param name="cookieName"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void AddCookie(string cookieName, string key, string value)
- {
- HttpCookie cookie = new HttpCookie(cookieName);
- cookie.Values.Add(key, value);
- AddCookie(cookie);
- }
-
- /// <summary>
- /// 添加为Cookie集合
- /// </summary>
- /// <param name="cookieName">Cookie名称</param>
- /// <param name="expires">过期时间</param>
- public static void AddCookie(string cookieName, DateTime expires)
- {
- HttpCookie cookie = new HttpCookie(cookieName);
- cookie.Expires = expires;
- AddCookie(cookie);
- }
-
- /// <summary>
- /// 添加为Cookie.Values集合
- /// </summary>
- /// <param name="cookieName"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="expires"></param>
- public static void AddCookie(string cookieName, string key, string value, DateTime expires)
- {
- HttpCookie cookie = new HttpCookie(cookieName);
- cookie.Expires = expires;
- cookie.Values.Add(key, value);
- AddCookie(cookie);
- }
-
- /// <summary>
- /// 添加Cookie
- /// </summary>
- /// <param name="cookie"></param>
- public static void AddCookie(HttpCookie cookie)
- {
- HttpResponse response = HttpContext.Current.Response;
- if (response != null)
- {
- //指定客户端脚本是否可以访问[默认为false]
- cookie.HttpOnly = true;
- //指定统一的Path,比便能通存通取
- cookie.Path = "/";
- //设置跨域,这样在其它二级域名下就都可以访问到了
- //cookie.Domain = "chinesecoo.com";
- response.AppendCookie(cookie);
- }
- }
-
- #endregion
- }
- }
|
请发表评论