WebCookie.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Web;
  3. namespace SiteCore
  4. {
  5. public class WebCookie
  6. {
  7. private const string ViewCookieName = "ViewCookie";
  8. public static void CookieLogin(string username, string ticket, int expiresDay)
  9. {
  10. CookieLogin(username, ticket, 0, expiresDay);
  11. }
  12. //登录
  13. public static void CookieLogin(string username, string ticket, int type, int expiresDay)
  14. {
  15. HttpCookie cookie = new HttpCookie(webConfig.CookieName);
  16. cookie.Values.Clear();
  17. cookie.Values.Add("User", username);
  18. cookie.Values.Add("Ticket", ticket);
  19. cookie.Values.Add("ST", type.ToString());
  20. cookie.Path = "/";
  21. cookie.Domain = webConfig.SiteDomain;
  22. if (expiresDay > 0) cookie.Expires = DateTime.Now.AddDays(expiresDay);
  23. HttpContext.Current.Response.Cookies.Add(cookie);
  24. //清队缓存
  25. WebUser.RemoveUserCache(username);
  26. }
  27. //退出
  28. public static void CookieLogout()
  29. {
  30. HttpCookie cookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  31. cookie.Domain = webConfig.SiteDomain;
  32. cookie.Path = "/";
  33. cookie.Expires = DateTime.Now.AddDays(-1d);
  34. HttpContext.Current.Response.Cookies.Add(cookie);
  35. }
  36. public static string GetViewCookies(string key)
  37. {
  38. HttpCookie cookie = HttpContext.Current.Request.Cookies[ViewCookieName];
  39. if (cookie != null)
  40. return cookie.Values[key];
  41. return "";
  42. }
  43. public static void SetViewCookies(string key, string value)
  44. {
  45. HttpCookie cookie = HttpContext.Current.Request.Cookies[ViewCookieName];
  46. if (cookie == null)
  47. {
  48. cookie = new HttpCookie(ViewCookieName);
  49. cookie.Expires = DateTime.Now.AddDays(1);
  50. }
  51. if (!string.IsNullOrEmpty(cookie.Values[key]))
  52. {
  53. cookie.Values[key] = value;
  54. }
  55. else
  56. {
  57. cookie.Values.Add(key, value);
  58. }
  59. HttpContext.Current.Response.Cookies.Set(cookie);
  60. }
  61. /// <summary>
  62. /// 设置Cookie
  63. /// </summary>
  64. /// <param name="key"></param>
  65. /// <param name="value"></param>
  66. public static void SetCookie(string key, string value)
  67. {
  68. HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
  69. if (cookie == null) cookie = new HttpCookie(key);
  70. cookie.Value = value;
  71. HttpContext.Current.Response.Cookies.Set(cookie);
  72. }
  73. /// <summary>
  74. /// 获取Cookie
  75. /// </summary>
  76. /// <param name="key"></param>
  77. /// <returns></returns>
  78. public static string GetCookie(string key)
  79. {
  80. HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
  81. if (cookie != null)
  82. return cookie.Value;
  83. return "";
  84. }
  85. public static void DelCookie(string key)
  86. {
  87. if (key == "") return;
  88. HttpCookie cookie = new HttpCookie(key);
  89. cookie.Expires = DateTime.Now.AddDays(-1d);
  90. HttpContext.Current.Response.Cookies.Add(cookie);
  91. }
  92. }
  93. }