以下是类:
public class CookieMgr { #region 快速储存Cookie /// <summary> /// 快速储存Cookie /// </summary> /// <param name="dt">数据源表</param> /// <param name="cookieName">Cookie名称</param> /// <param name="day">过期时间,天</param> public static void SaveCookie(DataTable dt, string cookieName, int day) { HttpCookie cookie = new HttpCookie(cookieName); if (day != 0) cookie.Expires = DateTime.Now.AddDays(day); for (int i = 0; i < dt.Columns.Count; i++) { cookie.Values.Add(dt.Columns[i].ColumnName, StringMgr.Encrypt(dt.Rows[0][dt.Columns[i].ColumnName].ToString())); //加密 } HttpContext.Current.Response.AppendCookie(cookie); } #endregion #region 初始化一个Cookie /// <summary> /// 初始化一个Cookie /// </summary> /// <param name="cookieName">cookie名称</param> /// <param name="day">过期时间,天</param> public static void InitCookie(string cookieName, int day) { HttpCookie cookie = new HttpCookie(cookieName); if (day != 0) cookie.Expires = DateTime.Now.AddDays(day); cookie.Values.Add("initExpires", StringMgr.Encrypt(day.ToString())); HttpContext.Current.Response.Cookies.Add(cookie); } #endregion #region 设置Cookie的其中一个值,如果键存在则覆盖以前的键值 /// <summary> /// 附加一个Cookie的值,如果键值存在则覆盖以前的键值 /// </summary> /// <param name="cookieName">Cookie名称</param> /// <param name="key">需附加的键</param> /// <param name="value">需附加的值</param> public static void SetCookie(string cookieName, string key, string value) { HttpCookie cookie = null; if (HttpContext.Current.Request.Cookies[cookieName] != null) { cookie = HttpContext.Current.Request.Cookies[cookieName]; } else cookie = new HttpCookie(cookieName); bool b = false; for (int i = 0; i < cookie.Values.Keys.Count; i++) { if (cookie.Values.Keys[i] == key) { b = true; break; } } value = StringMgr.Encrypt(value); if (b) cookie.Values[key] = value; else cookie.Values.Add(key, value); for (int i = 0; i < cookie.Values.Keys.Count; i++) { if (cookie.Values.Keys[i] == "initExpires") { cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString()))); break; } } HttpContext.Current.Response.Cookies.Add(cookie); } #endregion #region 读取Cookie的其中一个值 /// <summary> /// 读取Cookie的其中一个值 /// </summary> /// <param name="cookieName">Cookie名称</param> /// <param name="key">需读取的键</param> /// <returns>返回名为【CookieName】键为【key】的值</returns> public static string GetCookie(string cookieName, string key) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie != null && cookie.Values[key] != null) return StringMgr.Decrypt(cookie.Values[key].ToString()); else return Param.CookieErrorString; } #endregion #region 移除Cookie的其中一个值 /// <summary> /// 移除Cookie的其中一个值 /// </summary> /// <param name="cookieName">Cookie名称</param> /// <param name="key">需附加的键</param> public static void RemoveCookie(string cookieName, string key) { HttpCookie cookie = null; if (HttpContext.Current.Request.Cookies[cookieName] != null) cookie = HttpContext.Current.Request.Cookies[cookieName]; else cookie = new HttpCookie(cookieName); bool b = false; for (int i = 0; i < cookie.Values.Keys.Count; i++) { if (cookie.Values.Keys[i] == key) { b = true; break; } } if (b) cookie.Values.Remove(key); for (int i = 0; i < cookie.Values.Keys.Count; i++) { if (cookie.Values.Keys[i] == "initExpires") { cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString()))); break; } } HttpContext.Current.Response.Cookies.Add(cookie); } #endregion #region 清空Cookie /// <summary> /// 清空Cookie /// </summary> /// <param name="cookieName">Cookie名称</param> public static void ClearCookie(string cookieName) { System.Web.HttpCookie cookie = new System.Web.HttpCookie(cookieName); cookie.Expires = System.DateTime.Now.AddYears(-1); System.Web.HttpContext.Current.Response.AppendCookie(cookie); } #endregion #region 得到Cookie的集合 /// <summary> /// 得到Cookie的集合 /// </summary> /// <param name="cookieName">Cookie名称</param> /// <returns>返回Cookie的集合</returns> public static NameValueCollection GetCookieValues(string cookieName) { if (HttpContext.Current.Request.Cookies[cookieName] != null) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; return cookie.Values; } else return null; } #endregion }
保存DT到cookie
ookieMgr.SaveCookie(dt, "名称", 0);
获取cookie key值
CookieMgr.GetCookie("名称", "姓名");