Command_Cookie.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Web;
  16. using System.Web.Security;
  17. namespace iNethinkCMS.Command
  18. {
  19. public class Command_Cookie
  20. {
  21. /// <summary>
  22. /// 保存Cookie
  23. /// </summary>
  24. /// <param name="CookieName">Cookie名称</param>
  25. /// <param name="CookieValue">Cookie值</param>
  26. /// <param name="CookieTime">Cookie过期时间(分钟),0为关闭页面失效</param>
  27. public static void SaveCookie(string CookieName, string CookieValue, double CookieTime)
  28. {
  29. HttpCookie myCookie = new HttpCookie(CookieName);
  30. DateTime now = DateTime.Now;
  31. myCookie.Value = CookieValue;
  32. if (CookieTime != 0)
  33. {
  34. myCookie.Expires = now.AddHours(CookieTime);
  35. if (HttpContext.Current.Response.Cookies[CookieName] != null)
  36. {
  37. HttpContext.Current.Response.Cookies.Remove(CookieName);
  38. }
  39. HttpContext.Current.Response.Cookies.Add(myCookie);
  40. }
  41. else
  42. {
  43. if (HttpContext.Current.Response.Cookies[CookieName] != null)
  44. {
  45. HttpContext.Current.Response.Cookies.Remove(CookieName);
  46. }
  47. HttpContext.Current.Response.Cookies.Add(myCookie);
  48. }
  49. }
  50. /// <summary>
  51. /// 取得CookieValue
  52. /// </summary>
  53. /// <param name="CookieName">Cookie名称</param>
  54. /// <returns>Cookie的值</returns>
  55. public static string GetCookie(string CookieName)
  56. {
  57. HttpCookie myCookie = new HttpCookie(CookieName);
  58. myCookie = HttpContext.Current.Request.Cookies[CookieName];
  59. if (myCookie != null)
  60. {
  61. return myCookie.Value;
  62. }
  63. else
  64. {
  65. return null;
  66. }
  67. }
  68. /// <summary>
  69. /// 清除CookieValue
  70. /// </summary>
  71. /// <param name="CookieName">Cookie名称</param>
  72. public static void ClearCookie(string CookieName)
  73. {
  74. HttpCookie myCookie = new HttpCookie(CookieName);
  75. DateTime now = DateTime.Now;
  76. myCookie.Expires = now.AddYears(-2);
  77. HttpContext.Current.Response.Cookies.Add(myCookie);
  78. }
  79. }
  80. }