CookieHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /******************************************************************************
  2. * 作者: 季健国
  3. * 创建时间: 2012/6/6 22:19:49
  4. *
  5. *
  6. ******************************************************************************/
  7. using System;
  8. using System.Web;
  9. namespace Ant.Service.Common
  10. {
  11. /// <summary>
  12. /// Cookie辅助类
  13. /// </summary>
  14. public class CookieHelper
  15. {
  16. /// <summary>
  17. /// 清除指定Cookie
  18. /// </summary>
  19. /// <param name="cookiename">cookiename</param>
  20. public static void ClearCookie(string cookiename)
  21. {
  22. HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
  23. if (cookie != null)
  24. {
  25. cookie.Expires = DateTime.Now.AddDays(-14400);
  26. HttpContext.Current.Response.Cookies.Remove(cookiename);
  27. HttpContext.Current.Response.AppendCookie(cookie);
  28. }
  29. }
  30. /// <summary>
  31. /// 获取指定Cookie值
  32. /// </summary>
  33. /// <param name="cookiename">cookiename</param>
  34. /// <returns></returns>
  35. public static string GetCookieValue(string cookiename)
  36. {
  37. HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
  38. string str = string.Empty;
  39. if (cookie != null)
  40. {
  41. str = cookie.Value;
  42. }
  43. return str;
  44. }
  45. /// <summary>
  46. /// 获取cookie
  47. /// </summary>
  48. /// <param name="cookiename"></param>
  49. /// <returns></returns>
  50. public static HttpCookie GetCookie(string cookiename)
  51. {
  52. return HttpContext.Current.Request.Cookies[cookiename];
  53. }
  54. /// <summary>
  55. /// 添加一个Cookie,默认浏览器关闭过期
  56. /// </summary>
  57. public static void SetCookie(string cookiename, System.Collections.Specialized.NameValueCollection cookievalue,int? days)
  58. {
  59. var cookie = HttpContext.Current.Request.Cookies[cookiename];
  60. if (cookie == null)
  61. {
  62. cookie = new HttpCookie(cookiename);
  63. cookie.Values.Add(cookievalue);
  64. var siteurl = System.Configuration.ConfigurationManager.AppSettings["siteurl"];
  65. if (!string.IsNullOrEmpty(siteurl))
  66. {
  67. cookie.Domain = siteurl.Replace("www.", "");
  68. }
  69. if (days != null && days > 0) { cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(days)); }
  70. HttpContext.Current.Response.AppendCookie(cookie);
  71. }
  72. else
  73. {
  74. HttpContext.Current.Response.SetCookie(cookie);
  75. }
  76. }
  77. /// <summary>
  78. /// 添加一个Cookie
  79. /// </summary>
  80. /// <param name="cookiename">cookie名</param>
  81. /// <param name="cookievalue">cookie值</param>
  82. /// <param name="expires">过期时间 null为浏览器过期</param>
  83. public static void SetCookie(string cookiename, string cookievalue, int? expires)
  84. {
  85. var cookie = HttpContext.Current.Request.Cookies[cookiename];
  86. if (cookie == null)
  87. {
  88. cookie = new HttpCookie(cookiename);
  89. cookie.Value = cookievalue;
  90. var siteurl = System.Configuration.ConfigurationManager.AppSettings["siteurl"];
  91. if (!string.IsNullOrEmpty(siteurl))
  92. {
  93. cookie.Domain = siteurl.Replace("www.", "");
  94. }
  95. if (expires != null && expires > 0) { cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(expires)); }
  96. HttpContext.Current.Response.AppendCookie(cookie);
  97. }
  98. else
  99. {
  100. HttpContext.Current.Response.SetCookie(cookie);
  101. }
  102. }
  103. }
  104. }