CookieHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Globalization;
  3. using static System.Web.Configuration.WebConfigurationManager;
  4. using System.Web;
  5. namespace CommonTool
  6. {
  7. /// <summary>
  8. /// Cookie帮助类
  9. /// </summary>
  10. public class CookieHelper
  11. {
  12. public static CookieHelper Instance { get; } = new CookieHelper();
  13. /// <summary>
  14. /// 写cookie值
  15. /// </summary>
  16. /// <param name="strName">名称</param>
  17. /// <param name="strValue">值</param>
  18. public void WriteCookie(string strName, string strValue)
  19. {
  20. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName);
  21. cookie.Value = strValue;
  22. HttpContext.Current.Response.AppendCookie(cookie);
  23. }
  24. /// <summary>
  25. /// 写cookie值
  26. /// </summary>
  27. /// <param name="strName">名称</param>
  28. /// <param name="strValue">值</param>
  29. /// <param name="expires">过期时间(分钟)</param>
  30. public void WriteCookie(string strName, string strValue, int expires)
  31. {
  32. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName);
  33. cookie.Value = strValue;
  34. cookie.Expires = DateTime.Now.AddMinutes(expires);
  35. HttpContext.Current.Response.AppendCookie(cookie);
  36. }
  37. /// <summary>
  38. /// 读cookie值
  39. /// </summary>
  40. /// <param name="strName">名称</param>
  41. /// <returns>cookie值</returns>
  42. public string GetCookie(string strName)
  43. {
  44. if (HttpContext.Current.Request.Cookies[strName] != null)
  45. {
  46. return HttpContext.Current.Request.Cookies[strName].Value;
  47. }
  48. return "";
  49. }
  50. /// <summary>
  51. /// Get cookie expiry date that was set in the cookie value
  52. /// </summary>
  53. /// <param name="cookie"></param>
  54. /// <returns></returns>
  55. public DateTime GetExpirationDate(HttpCookie cookie)
  56. {
  57. if (String.IsNullOrEmpty(cookie.Value))
  58. {
  59. return DateTime.MinValue;
  60. }
  61. string strDateTime = cookie.Value.Substring(cookie.Value.IndexOf("|", StringComparison.Ordinal) + 1);
  62. return Convert.ToDateTime(strDateTime);
  63. }
  64. /// <summary>
  65. /// Set cookie value using the token and the expiry date
  66. /// </summary>
  67. /// <param name="value"></param>
  68. /// <param name="minutes"></param>
  69. /// <returns></returns>
  70. public string BuildCookueValue(string value, int minutes)
  71. {
  72. return $"{value}|{DateTime.Now.AddMinutes(minutes).ToString(CultureInfo.InvariantCulture)}";
  73. }
  74. /// <summary>
  75. /// Reads cookie value from the cookie
  76. /// </summary>
  77. /// <param name="cookie"></param>
  78. /// <returns></returns>
  79. public string GetCookieValue(HttpCookie cookie)
  80. {
  81. if (cookie!=null)
  82. {
  83. if (String.IsNullOrEmpty(cookie.Value))
  84. return cookie.Value;
  85. return cookie.Value.Substring(0, cookie.Value.IndexOf("|", StringComparison.Ordinal));
  86. }
  87. return null;
  88. }
  89. public void DelCookie(string strName)
  90. {
  91. WriteCookie(strName, "", -10000);
  92. }
  93. public SysUserInfo GetSysUserInfo(string pcCookieName)
  94. {
  95. string lcCookieName = pcCookieName?? AppSettings["SysUserInfoCookie"];
  96. string lcCookieValue = GetCookieValue(HttpContext.Current.Request.Cookies[lcCookieName]);
  97. try
  98. {
  99. return JsonHelper.Instance.Deserialize<SysUserInfo>(SysSecurity.Decrypt(lcCookieValue));
  100. }
  101. catch
  102. {
  103. return null;
  104. }
  105. }
  106. }
  107. }