using System; using System.Collections.Generic; using System.Web; using System.Data; using System.Runtime.Serialization; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Configuration; namespace Ant.Service.Utilities { /// /// Cookie操作类 /// public class CookieSession { private static string domain = ConfigurationManager.AppSettings["domain"].Trim(); #region 对实体类进行序列化和反序列化 /// /// 将对象序列化 /// /// /// private static MemoryStream Serialize(object obj) { try { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); formatter.Serialize(ms, obj); return ms; } catch { return null; } } /// /// /// /// /// /// private static Stream Serialize(object obj, Stream stream) { try { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); return stream; } catch { return null; } } /// /// 序列化 /// /// /// private static object Deserialize(Stream stream) { try { BinaryFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(stream); } catch { return null; } } /// /// 将实体或datatable转换为string /// /// /// private static string SerializeToString(object obj) { try { MemoryStream msObj = Serialize(obj); return Convert.ToBase64String(msObj.ToArray()); } catch { return null; } } /// /// 将string转换为对象 /// /// /// private static object DeserializeFromString(string str) { try { byte[] byteArrayObj = Convert.FromBase64String(str); Stream myObj = new MemoryStream(byteArrayObj); return Deserialize(myObj); } catch { return null; } } #endregion #region cookies的读取和添加 /// /// 将DTO插入cookies 关闭浏览器后失效 /// /// cookies名称 /// cookies对象 public static void InnerCookieNoEx(string cname, object DataTab) { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { hc = new HttpCookie(cname); } // hc.Expires = DateTime.Now.AddDays(14); hc.Value = AES.Encode(SerializeToString(DataTab)); hc.Domain = domain; HttpContext.Current.Response.Cookies.Add(hc); } /// /// 将DTO插入cookies 默认14天 /// /// cookies名称 /// cookies对象 public static void InnerCookie(string cname, object DataTab) { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { hc = new HttpCookie(cname); } hc.Expires = DateTime.Now.AddDays(14); hc.Value = AES.Encode(SerializeToString(DataTab)); hc.Domain = domain; HttpContext.Current.Response.Cookies.Add(hc); } /// /// 写入cookies /// /// cookies的名称 /// cookies的对象,可为实体,datatable /// 持续分钟 public static void InnerCookie(string cname, object DataTab, int mins) { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { hc = new HttpCookie(cname); } hc.Expires = DateTime.Now.AddMinutes(mins); hc.Value = AES.Encode(SerializeToString(DataTab)); hc.Domain = domain; HttpContext.Current.Response.Cookies.Add(hc); } /// /// 通过cookies的名称取得cookies /// 注意判断string.empty和null /// /// cookies的名称 /// public static object GetCookies(string cname) { try { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { return string.Empty; } else { return DeserializeFromString(AES.Decode(hc.Value)); } } catch { return null; } } /// /// 删除 指定 cookies /// /// cookies的名称 /// public static void DelCookiesByName(string cname) { HttpCookie myCookie = HttpContext.Current.Request.Cookies[cname]; if (myCookie != null) { myCookie.Expires = DateTime.Now.AddDays(-14); myCookie.Value = null; myCookie.Domain = domain; HttpContext.Current.Response.Cookies.Add(myCookie); } else { myCookie = new HttpCookie(cname); myCookie.Expires = DateTime.Now.AddDays(-14); myCookie.Value = null; myCookie.Domain = domain; HttpContext.Current.Response.Cookies.Add(myCookie); } } #endregion } }