CookieHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Common.Http;
  2. using Microsoft.AspNetCore.Http;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web;
  8. //using Liger.Common;
  9. namespace LigerRM.Common
  10. {
  11. /// <summary>
  12. /// 不支持 cookie values
  13. /// </summary>
  14. public class CookieHelper
  15. {
  16. #region 获取Cookie
  17. /// <summary>
  18. /// 获得Cookie的值
  19. /// </summary>
  20. /// <param name="cookieName"></param>
  21. /// <returns></returns>
  22. public static string GetCookieValue(string cookieName)
  23. {
  24. HttpRequest request = MyHttpContext.Current.Request;
  25. if (request != null)
  26. return request.Cookies[cookieName];
  27. return "";
  28. }
  29. /// <summary>
  30. /// 获得Cookie的值
  31. /// </summary>
  32. /// <param name="cookie"></param>
  33. /// <returns></returns>
  34. //public static string GetCookieValue(HttpCookie cookie)
  35. //{
  36. // if (cookie != null)
  37. // {
  38. // return cookie.Value;
  39. // }
  40. // return "";
  41. //}
  42. /// <summary>
  43. /// 获得Cookie
  44. /// </summary>
  45. /// <param name="cookieName"></param>
  46. /// <returns></returns>
  47. public static string GetCookie(string cookieName)
  48. {
  49. HttpRequest request = MyHttpContext.Current.Request;
  50. if (request != null)
  51. return request.Cookies[cookieName];
  52. return null;
  53. }
  54. #endregion
  55. #region 删除Cookie
  56. /// <summary>
  57. /// 删除Cookie
  58. /// </summary>
  59. /// <param name="cookieName"></param>
  60. public static void RemoveCookie(string cookieName)
  61. {
  62. HttpResponse response = MyHttpContext.Current.Response;
  63. if (response != null)
  64. {
  65. response.Cookies.Delete(cookieName);
  66. }
  67. }
  68. #endregion
  69. #region 设置/修改Cookie
  70. /// <summary>
  71. /// 设置Cookie
  72. /// </summary>
  73. /// <param name="cookieName"></param>
  74. /// <param name="key"></param>
  75. /// <param name="value"></param>
  76. /// <param name="expires"></param>
  77. public static void SetCookie(string cookieName,string value, DateTime? expires)
  78. {
  79. //Guard.IsNotNullOrEmpty(cookieName, "cookieName");
  80. HttpResponse response = MyHttpContext.Current.Response;
  81. if (response != null)
  82. {
  83. response.Cookies.Delete(cookieName);
  84. response.Cookies.Append(cookieName, value, new CookieOptions
  85. {
  86. Expires = expires
  87. });
  88. }
  89. }
  90. #endregion
  91. #region 添加Cookie
  92. /// <summary>
  93. /// 添加为Cookie.Values集合
  94. /// </summary>
  95. /// <param name="cookieName"></param>
  96. /// <param name="key"></param>
  97. /// <param name="value"></param>
  98. /// <param name="expires"></param>
  99. public static void AddCookie(string cookieName, string value, DateTime expires)
  100. {
  101. //Guard.IsNotNullOrEmpty(cookieName, "cookieName");
  102. //HttpCookie cookie = new HttpCookie(cookieName);
  103. //cookie.Expires = expires;
  104. //cookie.Value = value;
  105. //AddCookie(cookie);
  106. HttpResponse response = MyHttpContext.Current.Response;
  107. response.Cookies.Append(cookieName, value, new CookieOptions
  108. {
  109. Expires = expires
  110. });
  111. }
  112. /// <summary>
  113. /// 添加Cookie
  114. /// </summary>
  115. /// <param name="cookie"></param>
  116. //public static void AddCookie(HttpCookie cookie)
  117. //{
  118. // HttpResponse response = MyHttpContext.Current.Response;
  119. // if (response != null)
  120. // {
  121. // //指定客户端脚本是否可以访问[默认为false]
  122. // cookie.HttpOnly = true;
  123. // //指定统一的Path,比便能通存通取
  124. // cookie.Path = "/";
  125. // //设置跨域,这样在其它二级域名下就都可以访问到了
  126. // //cookie.Domain = "nas.com";
  127. // response.AppendCookie(cookie);
  128. // }
  129. //}
  130. #endregion
  131. }
  132. }