using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Configuration; using System.Web.Caching; namespace Ant.Service.Utilities { /// /// cookies操作类 /// public static class Cookies { /// /// cookies的作用域 /// //private static string domain = ".taocz.com"; private static string domain = ConfigurationManager.AppSettings["domain"].Trim(); #region 不加密写入cookies /// /// 写入cookies[默认不加密] /// /// cookies名称 /// cookies的值 /// 有效期 public static void InnerCookie(string cname, string values, int days) { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { hc = new HttpCookie(cname); } hc.Expires = DateTime.Now.AddDays(days); hc.Value = values; if (!string.IsNullOrEmpty(domain)) hc.Domain = domain; HttpContext.Current.Response.Cookies.Add(hc); } /// /// 给Cookies赋值 /// /// /// public static void SetCookie(string cname, string values) { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { hc = new HttpCookie(cname.ToString()); hc.Expires = DateTime.Now.AddDays(14); hc.Value = values; if (!string.IsNullOrEmpty(domain)) hc.Domain = domain; HttpContext.Current.Response.Cookies.Add(hc); } else { hc = new HttpCookie(cname); hc.Expires = DateTime.Now.AddDays(14); hc.Value = values; if (!string.IsNullOrEmpty(domain)) hc.Domain = domain; HttpContext.Current.Response.Cookies.Set(hc); } } /// /// 写入cookies默认14天时间 (两周)[默认不加密] /// /// cookies名称 /// cookies值 public static void InnerCookie(string cname, string values) { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { hc = new HttpCookie(cname); } hc.Expires = DateTime.Now.AddDays(14); hc.Value = values; if (!string.IsNullOrEmpty(domain)) hc.Domain = domain; HttpContext.Current.Response.Cookies.Add(hc); } #endregion #region 不解密读取cookies /// /// 通过cookies的名称获取cookies的值[默认不解密] /// /// /// public static string GetCookieValue(string cname) { try { HttpCookie hc = HttpContext.Current.Request.Cookies.Get(cname); if (hc == null) { return string.Empty; } else { return hc.Value; } } catch { return null; } } #endregion #region 删除cookies /// /// 删除 指定 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 #region session的读取和添加删除 /// 添加Session,调动有效期默认为23分钟 /// /// 添加Session,调动有效期默认为23分钟 /// /// Session对象名称 /// Session值 public static void AddSession(string strSessionName, string strValue) { HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = 23; } /// 添加Session /// /// 添加Session /// /// Session对象名称 /// Session值 /// 调动有效期(分钟) public static void AddSession(string strSessionName, string strValue, int Timeout) { HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = Timeout; } /// 读取某个Session对象值 /// /// 读取某个Session对象值 /// /// Session对象名称 /// Session对象值 public static string GetSession(string strSessionName) { if (HttpContext.Current.Session[strSessionName] == null) { return null; } else { return HttpContext.Current.Session[strSessionName].ToString(); } } /// /// 读取某个Session对象值数组 /// /// Session对象名称 /// Session对象值数组 public static string[] GetsSession(string strSessionName) { if (HttpContext.Current.Session[strSessionName] == null) { return null; } else { return (string[])HttpContext.Current.Session[strSessionName]; } } /// 删除某个Session对象 /// /// 删除某个Session对象 /// /// Session对象名称 public static void DelSession(string strSessionName) { HttpContext.Current.Session[strSessionName] = null; } /// /// 清空所有Session对象 /// public static void DelAllSession() { HttpContext.Current.Session.Abandon(); } #endregion #region 操作cache类 /// Cache操作类 /// /// Cache操作类 /// public static class CacheCustom { /// 简单创建/修改Cache,前提是这个值是字符串形式的 /// /// 简单创建/修改Cache,前提是这个值是字符串形式的 /// /// Cache名称 /// Cache值 /// 有效期,秒数(使用的是当前时间+秒数得到一个绝对到期值) /// 保留优先级,1最不会被清除,6最容易被内存管理清除(1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low) public static void InsertCache(string strCacheName, string strValue, int iExpires, int priority) { TimeSpan ts = new TimeSpan(0, 0, iExpires); CacheItemPriority cachePriority; switch (priority) { case 6: cachePriority = CacheItemPriority.Low; break; case 5: cachePriority = CacheItemPriority.BelowNormal; break; case 4: cachePriority = CacheItemPriority.Normal; break; case 3: cachePriority = CacheItemPriority.AboveNormal; break; case 2: cachePriority = CacheItemPriority.High; break; case 1: cachePriority = CacheItemPriority.NotRemovable; break; default: cachePriority = CacheItemPriority.Default; break; } HttpContext.Current.Cache.Insert(strCacheName, strValue, null, DateTime.Now.Add(ts), System.Web.Caching.Cache.NoSlidingExpiration, cachePriority, null); } /// 简单读书Cache对象的值,前提是这个值是字符串形式的 /// /// 简单读书Cache对象的值,前提是这个值是字符串形式的 /// /// Cache名称 /// Cache字符串值 public static string GetCache(string strCacheName) { return HttpContext.Current.Cache[strCacheName].ToString(); } /// 删除Cache对象 /// /// 删除Cache对象 /// /// Cache名称 public static void DelCache(string strCacheName) { HttpContext.Current.Cache.Remove(strCacheName); } } #endregion } }