[[高手答疑]] 【C#、Asp.Net 工具类汇总】Cookie常见操作类

[复制链接]
查看: 73|回复: 0
发表于 2024-9-15 01:10:28 | 显示全部楼层 | 阅读模式
易博V9下载

使用实例:

  1. private User u = new User();
  2. private string urlPath =
  3. protected void Page_Load(object sender, EventArgs e)
  4. if (!Page.IsPostBack)
  5. {
  6. #region 基础数据
  7. u = new User();
  8. u.UserName = "慈众营销";
  9. u.UserAge = 27;
  10. u.UserSex = "男";
  11. u.UserSite = "http://www.zhengdecai.com";
  12. urlPath = Server.MapPath("~/file/");
  13. #endregion
  14. cookieInfo();
  15. }
  16. ///<summary>
  17. /// Cookie测试
  18. ///</summary>
  19. private void cookieInfo()
  20. CookieHelper.SetCookie("userName", "慈众营销");
  21. CookieHelper.SetCookie("userAge", "27", DateTime.Now.AddDays(1));
  22. CookieHelper.WriteCookie("userSex", "男");
  23. CookieHelper.WriteCookie("userSite", "http://www.zhengdecai.com", 20);
  24. //CookieHelper.SetCookie("u", u);
  25. CookieHelper.ClearCookie("userName");
  26. CookieHelper.RemoveCookie("userAge");
  27. string cStr = CookieHelper.GetCookieValue("userName") + " - " + CookieHelper.GetCookieValue("userAge") + " - " + CookieHelper.GetCookieValue("userSex") + " - " + CookieHelper.GetCookieValue("userSite");
  28. Response.Write("存在Cookie:" + cStr + "<br/>");
  29. CookieHelper.ClearCookie("userName");
  30. cStr = CookieHelper.GetCookieValue("userName") + " - " + CookieHelper.GetCookieValue("userAge") + " - " + CookieHelper.GetCookieValue("userSex") + " - " + CookieHelper.GetCookieValue("userSite");
  31. CookieHelper.ClearAllCookie();
  32. cStr = CookieHelper.GetCookieValue("userName") + " - " + CookieHelper.GetCookieValue("userAge") + " - " + CookieHelper.GetCookieValue("userSex") + " - " + CookieHelper.GetCookieValue("userSite");
复制代码

类库信息:

  1. ///<summary>
  2. /// Cookie 通用操作类
  3. /// 1、ClearCookie(string strCookieName),清除指定Cookie对象
  4. /// 2、ClearAllCookie(),清空所有的Cookie对象
  5. /// 3、RemoveCookie(string strCookieName),删除指定Session对象
  6. /// 4、GetCookieValue(string strCookieName),获取指定Cookie值
  7. /// 5、SetCookie(string strCookieName, string strCookieValue),设置一个Cookie(24小时过期)
  8. /// 6、SetCookie(string strCookieName, string strCookieValue, DateTime iExpires),设置一个Cookie,指定过期时间iExpires
  9. /// 7、WriteCookie(string strCookieName, string strCookieValue),设置Cookie值
  10. /// 8、WriteCookie(string strCookieName, string strCookieValue, int iExpires),设置Cookie值,设置过期时间iExpires分钟
  11. ///</summary>
  12. public class CookieHelper
  13. #region 清除Cookie信息
  14. ///<summary>
  15. /// 清除指定Cookie对象
  16. ///</summary>
  17. ///<param name="strCookieName">Cookie对象名</param>
  18. public static void ClearCookie(string strCookieName)
  19. HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
  20. if (Cookie != null)
  21. {
  22. Cookie.Expires = DateTime.Now.AddYears(-3);
  23. HttpContext.Current.Response.Cookies.Add(Cookie);
  24. }
  25. }
  26. ///<summary>
  27. /// 清空所有的Cookie对象
  28. ///</summary>
  29. public static void ClearAllCookie()
  30. HttpContext.Current.Request.Cookies.Clear();
  31. }
  32. ///<summary>
  33. /// 删除指定Session对象
  34. ///</summary>
  35. public static void RemoveCookie(string strCookieName)
  36. HttpContext.Current.Request.Cookies.Remove(strCookieName);
  37. }
  38. #endregion
  39. #region 获取、设置Cookie信息
  40. ///<summary>
  41. /// 获取指定Cookie值
  42. ///</summary>
  43. ///<param name="strCookieName">strCookieName</param>
  44. public static string GetCookieValue(string strCookieName)
  45. HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
  46. string str = string.Empty;
  47. if (Cookie != null)
  48. {
  49. str = Cookie.Value;
  50. }
  51. return str;
  52. }
  53. ///<summary>
  54. /// 设置一个Cookie(24小时过期)
  55. ///</summary>
  56. ///<param name="strCookieName"></param>
  57. ///<param name="strCookieValue"></param>
  58. public static void SetCookie(string strCookieName, string strCookieValue)
  59. SetCookie(strCookieName, strCookieValue, DateTime.Now.AddDays(1.0));
  60. }
  61. ///<summary>
  62. /// 设置一个Cookie,指定过期时间iExpires
  63. ///</summary>
  64. ///<param name="strCookieName">Cookie名</param>
  65. ///<param name="strCookieValue">Cookie值</param>
  66. ///<param name="iExpires">过期时间 DateTime</param>
  67. public static void SetCookie(string strCookieName, string strCookieValue, DateTime iExpires)
  68. HttpCookie Cookie = new HttpCookie(strCookieName)
  69. {
  70. Value = strCookieValue,
  71. Expires = iExpires
  72. };
  73. HttpContext.Current.Response.Cookies.Add(Cookie);
  74. }
  75. #endregion
  76. #region 设置HTTP Cookie信息
  77. ///<summary>
  78. /// 设置Cookie值
  79. ///</summary>
  80. ///<param name="strCookieName">名称</param>
  81. ///<param name="strCookieValue">值</param>
  82. public static void WriteCookie(string strCookieName, string strCookieValue)
  83. HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
  84. if (Cookie == null)
  85. {
  86. Cookie = new HttpCookie(strCookieName);
  87. }
  88. Cookie.Value = strCookieValue;
  89. HttpContext.Current.Response.AppendCookie(Cookie);
  90. }
  91. ///<summary>
  92. /// 设置Cookie值,设置过期时间iExpires分钟
  93. ///</summary>
  94. ///<param name="strCookieName">名称</param>
  95. ///<param name="strCookieValue">值</param>
  96. ///<param name="iExpires">过期时间(分钟)</param>
  97. public static void WriteCookie(string strCookieName, string strCookieValue, int iExpires)
  98. HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
  99. if (Cookie == null)
  100. {
  101. Cookie = new HttpCookie(strCookieName);
  102. }
  103. Cookie.Value = strCookieValue;
  104. Cookie.Expires = DateTime.Now.AddMinutes(iExpires);
  105. HttpContext.Current.Response.AppendCookie(Cookie);
  106. }
  107. #endregion
复制代码

以上类库内容来源互联网,站长稍作整理

易博软件介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1、请认真发帖,禁止回复纯表情,纯数字等无意义的内容!帖子内容不要太简单!
2、提倡文明上网,净化网络环境!抵制低俗不良违法有害信息。
3、如果你对主帖作者的帖子不屑一顾的话,请勿回帖。谢谢合作!
3、问答求助区发帖求助后,如有其他用户热心帮您解决问题后,请自觉点击设为最佳答案按钮。

 
 
QQ在线客服
QQ技术支持
工作时间:
8:00-18:00
软著登字:
1361266号
官方微信扫一扫
weixin

QQ|小黑屋|Archiver|慈众营销 ( 粤ICP备15049986号 )|网站地图

自动发帖软件 | 自动发帖器 | 营销推广软件 | 网络营销工具 | 网络营销软件 | 网站推广工具 | 网络推广软件 | 网络推广工具 | 网页推广软件 | 信息发布软件 | 网站推广工具 | 网页推广软件

Powered by Discuz! X3.4   © 2012-2020 Comsenz Inc.  慈众科技 - Collect from 深圳吉宝泰佛文化有限公司 公司地址:罗湖区黄贝街道深南东路集浩大厦A1403

返回顶部 返回列表