123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- 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
- {
- /// <summary>
- /// cookies操作类
- /// </summary>
- public static class Cookies
- {
- /// <summary>
- /// cookies的作用域
- /// </summary>
- //private static string domain = ".taocz.com";
- private static string domain = ConfigurationManager.AppSettings["domain"].Trim();
- #region 不加密写入cookies
- /// <summary>
- /// 写入cookies[默认不加密]
- /// </summary>
- /// <param name="cname">cookies名称</param>
- /// <param name="values">cookies的值</param>
- /// <param name="days">有效期</param>
- 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);
- }
- /// <summary>
- /// 给Cookies赋值
- /// </summary>
- /// <param name="cname"></param>
- /// <param name="values"></param>
- 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);
- }
- }
- /// <summary>
- /// 写入cookies默认14天时间 (两周)[默认不加密]
- /// </summary>
- /// <param name="cname">cookies名称</param>
- /// <param name="values">cookies值</param>
- 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
- /// <summary>
- /// 通过cookies的名称获取cookies的值[默认不解密]
- /// </summary>
- /// <param name="cname"></param>
- /// <returns></returns>
- 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
- /// <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
- #region session的读取和添加删除
- /// 添加Session,调动有效期默认为23分钟
- /// <summary>
- /// 添加Session,调动有效期默认为23分钟
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <param name="strValue">Session值</param>
- public static void AddSession(string strSessionName, string strValue)
- {
- HttpContext.Current.Session[strSessionName] = strValue;
- HttpContext.Current.Session.Timeout = 23;
- }
- /// 添加Session
- /// <summary>
- /// 添加Session
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <param name="strValue">Session值</param>
- /// <param name="iExpires">调动有效期(分钟)</param>
- public static void AddSession(string strSessionName, string strValue, int Timeout)
- {
- HttpContext.Current.Session[strSessionName] = strValue;
- HttpContext.Current.Session.Timeout = Timeout;
- }
- /// 读取某个Session对象值
- /// <summary>
- /// 读取某个Session对象值
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <returns>Session对象值</returns>
- public static string GetSession(string strSessionName)
- {
- if (HttpContext.Current.Session[strSessionName] == null)
- {
- return null;
- }
- else
- {
- return HttpContext.Current.Session[strSessionName].ToString();
- }
- }
- /// <summary>
- /// 读取某个Session对象值数组
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <returns>Session对象值数组</returns>
- public static string[] GetsSession(string strSessionName)
- {
- if (HttpContext.Current.Session[strSessionName] == null)
- {
- return null;
- }
- else
- {
- return (string[])HttpContext.Current.Session[strSessionName];
- }
- }
- /// 删除某个Session对象
- /// <summary>
- /// 删除某个Session对象
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- public static void DelSession(string strSessionName)
- {
- HttpContext.Current.Session[strSessionName] = null;
- }
- /// <summary>
- /// 清空所有Session对象
- /// </summary>
- public static void DelAllSession()
- {
- HttpContext.Current.Session.Abandon();
- }
- #endregion
- #region 操作cache类
- /// Cache操作类
- /// <summary>
- /// Cache操作类
- /// </summary>
- public static class CacheCustom
- {
- /// 简单创建/修改Cache,前提是这个值是字符串形式的
- /// <summary>
- /// 简单创建/修改Cache,前提是这个值是字符串形式的
- /// </summary>
- /// <param name="strCacheName">Cache名称</param>
- /// <param name="strValue">Cache值</param>
- /// <param name="iExpires">有效期,秒数(使用的是当前时间+秒数得到一个绝对到期值)</param>
- /// <param name="priority">保留优先级,1最不会被清除,6最容易被内存管理清除(1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low)</param>
- 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对象的值,前提是这个值是字符串形式的
- /// <summary>
- /// 简单读书Cache对象的值,前提是这个值是字符串形式的
- /// </summary>
- /// <param name="strCacheName">Cache名称</param>
- /// <returns>Cache字符串值</returns>
- public static string GetCache(string strCacheName)
- {
- return HttpContext.Current.Cache[strCacheName].ToString();
- }
- /// 删除Cache对象
- /// <summary>
- /// 删除Cache对象
- /// </summary>
- /// <param name="strCacheName">Cache名称</param>
- public static void DelCache(string strCacheName)
- {
- HttpContext.Current.Cache.Remove(strCacheName);
- }
- }
- #endregion
- }
- }
|