CookieSession.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Data;
  5. using System.Runtime.Serialization;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Text;
  9. using System.Configuration;
  10. namespace Ant.Service.Utilities
  11. {
  12. /// <summary>
  13. /// Cookie操作类
  14. /// </summary>
  15. public class CookieSession
  16. {
  17. private static string domain = ConfigurationManager.AppSettings["domain"].Trim();
  18. #region 对实体类进行序列化和反序列化
  19. /// <summary>
  20. /// 将对象序列化
  21. /// </summary>
  22. /// <param name="obj"></param>
  23. /// <returns></returns>
  24. private static MemoryStream Serialize(object obj)
  25. {
  26. try
  27. {
  28. BinaryFormatter formatter = new BinaryFormatter();
  29. MemoryStream ms = new MemoryStream();
  30. formatter.Serialize(ms, obj);
  31. return ms;
  32. }
  33. catch
  34. {
  35. return null;
  36. }
  37. }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="obj"></param>
  42. /// <param name="stream"></param>
  43. /// <returns></returns>
  44. private static Stream Serialize(object obj, Stream stream)
  45. {
  46. try
  47. {
  48. BinaryFormatter formatter = new BinaryFormatter();
  49. formatter.Serialize(stream, obj);
  50. return stream;
  51. }
  52. catch
  53. {
  54. return null;
  55. }
  56. }
  57. /// <summary>
  58. /// 序列化
  59. /// </summary>
  60. /// <param name="stream"></param>
  61. /// <returns></returns>
  62. private static object Deserialize(Stream stream)
  63. {
  64. try
  65. {
  66. BinaryFormatter formatter = new BinaryFormatter();
  67. return formatter.Deserialize(stream);
  68. }
  69. catch
  70. {
  71. return null;
  72. }
  73. }
  74. /// <summary>
  75. /// 将实体或datatable转换为string
  76. /// </summary>
  77. /// <param name="obj"></param>
  78. /// <returns></returns>
  79. private static string SerializeToString(object obj)
  80. {
  81. try
  82. {
  83. MemoryStream msObj = Serialize(obj);
  84. return Convert.ToBase64String(msObj.ToArray());
  85. }
  86. catch
  87. {
  88. return null;
  89. }
  90. }
  91. /// <summary>
  92. /// 将string转换为对象
  93. /// </summary>
  94. /// <param name="str"></param>
  95. /// <returns></returns>
  96. private static object DeserializeFromString(string str)
  97. {
  98. try
  99. {
  100. byte[] byteArrayObj = Convert.FromBase64String(str);
  101. Stream myObj = new MemoryStream(byteArrayObj);
  102. return Deserialize(myObj);
  103. }
  104. catch
  105. {
  106. return null;
  107. }
  108. }
  109. #endregion
  110. #region cookies的读取和添加
  111. /// <summary>
  112. /// 将DTO插入cookies 关闭浏览器后失效
  113. /// </summary>
  114. /// <param name="cname">cookies名称</param>
  115. /// <param name="DataTab">cookies对象</param>
  116. public static void InnerCookieNoEx(string cname, object DataTab)
  117. {
  118. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  119. if (hc == null)
  120. {
  121. hc = new HttpCookie(cname);
  122. }
  123. // hc.Expires = DateTime.Now.AddDays(14);
  124. hc.Value = AES.Encode(SerializeToString(DataTab));
  125. hc.Domain = domain;
  126. HttpContext.Current.Response.Cookies.Add(hc);
  127. }
  128. /// <summary>
  129. /// 将DTO插入cookies 默认14天
  130. /// </summary>
  131. /// <param name="cname">cookies名称</param>
  132. /// <param name="DataTab">cookies对象</param>
  133. public static void InnerCookie(string cname, object DataTab)
  134. {
  135. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  136. if (hc == null)
  137. {
  138. hc = new HttpCookie(cname);
  139. }
  140. hc.Expires = DateTime.Now.AddDays(14);
  141. hc.Value = AES.Encode(SerializeToString(DataTab));
  142. hc.Domain = domain;
  143. HttpContext.Current.Response.Cookies.Add(hc);
  144. }
  145. /// <summary>
  146. /// 写入cookies
  147. /// </summary>
  148. /// <param name="cname">cookies的名称</param>
  149. /// <param name="DataTab">cookies的对象,可为实体,datatable</param>
  150. /// <param name="mins">持续分钟</param>
  151. public static void InnerCookie(string cname, object DataTab, int mins)
  152. {
  153. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  154. if (hc == null)
  155. {
  156. hc = new HttpCookie(cname);
  157. }
  158. hc.Expires = DateTime.Now.AddMinutes(mins);
  159. hc.Value = AES.Encode(SerializeToString(DataTab));
  160. hc.Domain = domain;
  161. HttpContext.Current.Response.Cookies.Add(hc);
  162. }
  163. /// <summary>
  164. /// 通过cookies的名称取得cookies
  165. /// 注意判断string.empty和null
  166. /// </summary>
  167. /// <param name="cname">cookies的名称</param>
  168. /// <returns></returns>
  169. public static object GetCookies(string cname)
  170. {
  171. try
  172. {
  173. HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname);
  174. if (hc == null)
  175. {
  176. return string.Empty;
  177. }
  178. else
  179. {
  180. return DeserializeFromString(AES.Decode(hc.Value));
  181. }
  182. }
  183. catch
  184. {
  185. return null;
  186. }
  187. }
  188. /// <summary>
  189. /// 删除 指定 cookies
  190. /// </summary>
  191. /// <param name="cname">cookies的名称</param>
  192. /// <returns></returns>
  193. public static void DelCookiesByName(string cname)
  194. {
  195. HttpCookie myCookie = HttpContext.Current.Request.Cookies[cname];
  196. if (myCookie != null)
  197. {
  198. myCookie.Expires = DateTime.Now.AddDays(-14);
  199. myCookie.Value = null;
  200. myCookie.Domain = domain;
  201. HttpContext.Current.Response.Cookies.Add(myCookie);
  202. }
  203. else
  204. {
  205. myCookie = new HttpCookie(cname);
  206. myCookie.Expires = DateTime.Now.AddDays(-14);
  207. myCookie.Value = null;
  208. myCookie.Domain = domain;
  209. HttpContext.Current.Response.Cookies.Add(myCookie);
  210. }
  211. }
  212. #endregion
  213. }
  214. }