CookiesSessionCache.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Configuration;
  7. using System.Web.Caching;
  8. namespace Ant.Service.Utilities
  9. {
  10. /// <summary>
  11. /// cookies操作类
  12. /// </summary>
  13. public static class Cookies
  14. {
  15. /// <summary>
  16. /// cookies的作用域
  17. /// </summary>
  18. //private static string domain = ".taocz.com";
  19. private static string domain = ConfigurationManager.AppSettings["domain"].Trim();
  20. #region 不加密写入cookies
  21. /// <summary>
  22. /// 写入cookies[默认不加密]
  23. /// </summary>
  24. /// <param name="cname">cookies名称</param>
  25. /// <param name="values">cookies的值</param>
  26. /// <param name="days">有效期</param>
  27. public static void InnerCookie(string cname, string values, int days)
  28. {
  29. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  30. if (hc == null)
  31. {
  32. hc = new HttpCookie(cname);
  33. }
  34. hc.Expires = DateTime.Now.AddDays(days);
  35. hc.Value = values;
  36. if (!string.IsNullOrEmpty(domain))
  37. hc.Domain = domain;
  38. HttpContext.Current.Response.Cookies.Add(hc);
  39. }
  40. /// <summary>
  41. /// 给Cookies赋值
  42. /// </summary>
  43. /// <param name="cname"></param>
  44. /// <param name="values"></param>
  45. public static void SetCookie(string cname, string values)
  46. {
  47. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  48. if (hc == null)
  49. {
  50. hc = new HttpCookie(cname.ToString());
  51. hc.Expires = DateTime.Now.AddDays(14);
  52. hc.Value = values;
  53. if (!string.IsNullOrEmpty(domain))
  54. hc.Domain = domain;
  55. HttpContext.Current.Response.Cookies.Add(hc);
  56. }
  57. else
  58. {
  59. hc = new HttpCookie(cname);
  60. hc.Expires = DateTime.Now.AddDays(14);
  61. hc.Value = values;
  62. if (!string.IsNullOrEmpty(domain))
  63. hc.Domain = domain;
  64. HttpContext.Current.Response.Cookies.Set(hc);
  65. }
  66. }
  67. /// <summary>
  68. /// 写入cookies默认14天时间 (两周)[默认不加密]
  69. /// </summary>
  70. /// <param name="cname">cookies名称</param>
  71. /// <param name="values">cookies值</param>
  72. public static void InnerCookie(string cname, string values)
  73. {
  74. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  75. if (hc == null)
  76. {
  77. hc = new HttpCookie(cname);
  78. }
  79. hc.Expires = DateTime.Now.AddDays(14);
  80. hc.Value = values;
  81. if (!string.IsNullOrEmpty(domain))
  82. hc.Domain = domain;
  83. HttpContext.Current.Response.Cookies.Add(hc);
  84. }
  85. #endregion
  86. #region 不解密读取cookies
  87. /// <summary>
  88. /// 通过cookies的名称获取cookies的值[默认不解密]
  89. /// </summary>
  90. /// <param name="cname"></param>
  91. /// <returns></returns>
  92. public static string GetCookieValue(string cname)
  93. {
  94. try
  95. {
  96. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  97. if (hc == null)
  98. {
  99. return string.Empty;
  100. }
  101. else
  102. {
  103. return hc.Value;
  104. }
  105. }
  106. catch
  107. {
  108. return null;
  109. }
  110. }
  111. #endregion
  112. #region 删除cookies
  113. /// <summary>
  114. /// 删除 指定 cookies
  115. /// </summary>
  116. /// <param name="cname">cookies的名称</param>
  117. /// <returns></returns>
  118. public static void DelCookiesByName(string cname)
  119. {
  120. HttpCookie myCookie = HttpContext.Current.Request.Cookies[cname];
  121. if (myCookie != null)
  122. {
  123. myCookie.Expires = DateTime.Now.AddDays(-14);
  124. myCookie.Value = null;
  125. myCookie.Domain = domain;
  126. HttpContext.Current.Response.Cookies.Add(myCookie);
  127. }
  128. else
  129. {
  130. myCookie = new HttpCookie(cname);
  131. myCookie.Expires = DateTime.Now.AddDays(-14);
  132. myCookie.Value = null;
  133. myCookie.Domain = domain;
  134. HttpContext.Current.Response.Cookies.Add(myCookie);
  135. }
  136. }
  137. #endregion
  138. #region session的读取和添加删除
  139. /// 添加Session,调动有效期默认为23分钟
  140. /// <summary>
  141. /// 添加Session,调动有效期默认为23分钟
  142. /// </summary>
  143. /// <param name="strSessionName">Session对象名称</param>
  144. /// <param name="strValue">Session值</param>
  145. public static void AddSession(string strSessionName, string strValue)
  146. {
  147. HttpContext.Current.Session[strSessionName] = strValue;
  148. HttpContext.Current.Session.Timeout = 23;
  149. }
  150. /// 添加Session
  151. /// <summary>
  152. /// 添加Session
  153. /// </summary>
  154. /// <param name="strSessionName">Session对象名称</param>
  155. /// <param name="strValue">Session值</param>
  156. /// <param name="iExpires">调动有效期(分钟)</param>
  157. public static void AddSession(string strSessionName, string strValue, int Timeout)
  158. {
  159. HttpContext.Current.Session[strSessionName] = strValue;
  160. HttpContext.Current.Session.Timeout = Timeout;
  161. }
  162. /// 读取某个Session对象值
  163. /// <summary>
  164. /// 读取某个Session对象值
  165. /// </summary>
  166. /// <param name="strSessionName">Session对象名称</param>
  167. /// <returns>Session对象值</returns>
  168. public static string GetSession(string strSessionName)
  169. {
  170. if (HttpContext.Current.Session[strSessionName] == null)
  171. {
  172. return null;
  173. }
  174. else
  175. {
  176. return HttpContext.Current.Session[strSessionName].ToString();
  177. }
  178. }
  179. /// <summary>
  180. /// 读取某个Session对象值数组
  181. /// </summary>
  182. /// <param name="strSessionName">Session对象名称</param>
  183. /// <returns>Session对象值数组</returns>
  184. public static string[] GetsSession(string strSessionName)
  185. {
  186. if (HttpContext.Current.Session[strSessionName] == null)
  187. {
  188. return null;
  189. }
  190. else
  191. {
  192. return (string[])HttpContext.Current.Session[strSessionName];
  193. }
  194. }
  195. /// 删除某个Session对象
  196. /// <summary>
  197. /// 删除某个Session对象
  198. /// </summary>
  199. /// <param name="strSessionName">Session对象名称</param>
  200. public static void DelSession(string strSessionName)
  201. {
  202. HttpContext.Current.Session[strSessionName] = null;
  203. }
  204. /// <summary>
  205. /// 清空所有Session对象
  206. /// </summary>
  207. public static void DelAllSession()
  208. {
  209. HttpContext.Current.Session.Abandon();
  210. }
  211. #endregion
  212. #region 操作cache类
  213. /// Cache操作类
  214. /// <summary>
  215. /// Cache操作类
  216. /// </summary>
  217. public static class CacheCustom
  218. {
  219. /// 简单创建/修改Cache,前提是这个值是字符串形式的
  220. /// <summary>
  221. /// 简单创建/修改Cache,前提是这个值是字符串形式的
  222. /// </summary>
  223. /// <param name="strCacheName">Cache名称</param>
  224. /// <param name="strValue">Cache值</param>
  225. /// <param name="iExpires">有效期,秒数(使用的是当前时间+秒数得到一个绝对到期值)</param>
  226. /// <param name="priority">保留优先级,1最不会被清除,6最容易被内存管理清除(1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low)</param>
  227. public static void InsertCache(string strCacheName, string strValue, int iExpires, int priority)
  228. {
  229. TimeSpan ts = new TimeSpan(0, 0, iExpires);
  230. CacheItemPriority cachePriority;
  231. switch (priority)
  232. {
  233. case 6:
  234. cachePriority = CacheItemPriority.Low;
  235. break;
  236. case 5:
  237. cachePriority = CacheItemPriority.BelowNormal;
  238. break;
  239. case 4:
  240. cachePriority = CacheItemPriority.Normal;
  241. break;
  242. case 3:
  243. cachePriority = CacheItemPriority.AboveNormal;
  244. break;
  245. case 2:
  246. cachePriority = CacheItemPriority.High;
  247. break;
  248. case 1:
  249. cachePriority = CacheItemPriority.NotRemovable;
  250. break;
  251. default:
  252. cachePriority = CacheItemPriority.Default;
  253. break;
  254. }
  255. HttpContext.Current.Cache.Insert(strCacheName, strValue, null, DateTime.Now.Add(ts), System.Web.Caching.Cache.NoSlidingExpiration, cachePriority, null);
  256. }
  257. /// 简单读书Cache对象的值,前提是这个值是字符串形式的
  258. /// <summary>
  259. /// 简单读书Cache对象的值,前提是这个值是字符串形式的
  260. /// </summary>
  261. /// <param name="strCacheName">Cache名称</param>
  262. /// <returns>Cache字符串值</returns>
  263. public static string GetCache(string strCacheName)
  264. {
  265. return HttpContext.Current.Cache[strCacheName].ToString();
  266. }
  267. /// 删除Cache对象
  268. /// <summary>
  269. /// 删除Cache对象
  270. /// </summary>
  271. /// <param name="strCacheName">Cache名称</param>
  272. public static void DelCache(string strCacheName)
  273. {
  274. HttpContext.Current.Cache.Remove(strCacheName);
  275. }
  276. }
  277. #endregion
  278. }
  279. }