123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- 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
- {
- /// <summary>
- /// Cookie操作类
- /// </summary>
- public class CookieSession
- {
- private static string domain = ConfigurationManager.AppSettings["domain"].Trim();
- #region 对实体类进行序列化和反序列化
- /// <summary>
- /// 将对象序列化
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- private static MemoryStream Serialize(object obj)
- {
- try
- {
- BinaryFormatter formatter = new BinaryFormatter();
- MemoryStream ms = new MemoryStream();
- formatter.Serialize(ms, obj);
- return ms;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="obj"></param>
- /// <param name="stream"></param>
- /// <returns></returns>
- private static Stream Serialize(object obj, Stream stream)
- {
- try
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(stream, obj);
- return stream;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 序列化
- /// </summary>
- /// <param name="stream"></param>
- /// <returns></returns>
- private static object Deserialize(Stream stream)
- {
- try
- {
- BinaryFormatter formatter = new BinaryFormatter();
- return formatter.Deserialize(stream);
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 将实体或datatable转换为string
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- private static string SerializeToString(object obj)
- {
- try
- {
- MemoryStream msObj = Serialize(obj);
- return Convert.ToBase64String(msObj.ToArray());
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 将string转换为对象
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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的读取和添加
- /// <summary>
- /// 将DTO插入cookies 关闭浏览器后失效
- /// </summary>
- /// <param name="cname">cookies名称</param>
- /// <param name="DataTab">cookies对象</param>
- 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);
- }
- /// <summary>
- /// 将DTO插入cookies 默认14天
- /// </summary>
- /// <param name="cname">cookies名称</param>
- /// <param name="DataTab">cookies对象</param>
- 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);
- }
- /// <summary>
- /// 写入cookies
- /// </summary>
- /// <param name="cname">cookies的名称</param>
- /// <param name="DataTab">cookies的对象,可为实体,datatable</param>
- /// <param name="mins">持续分钟</param>
- 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);
- }
- /// <summary>
- /// 通过cookies的名称取得cookies
- /// 注意判断string.empty和null
- /// </summary>
- /// <param name="cname">cookies的名称</param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 删除 指定 cookies
- /// </summary>
- /// <param name="cname">cookies的名称</param>
- /// <returns></returns>
- 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
- }
-
- }
|