控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

ASP.NET 對cookies的操作

作者:佚名 出处:互联网 2011年10月19日 阅读:

ASP.NET 對cookies的操作


public class BsCookie 

   { 

       //操作的cookie 

       private HttpCookie _theCookie; 

       //對應的cookie的名稱 

       private string _cookieName; 

       private bool _httpOnly = true; 

       /// <summary> 

       /// 是否只允許在服務器端訪問,默認只允許在服務端訪問 

       /// </summary> 

       public bool HttpOnly 

       { 

           get { return _httpOnly; } 

           set { _httpOnly = value; } 

       } 

       private double _expireMinutes; 

       /// <summary> 

       /// Cookies有效的存放時間,以分鐘表示 

       /// </summary> 

       public double ExpireMinutes 

       { 

           get { return _expireMinutes; } 

           set { _expireMinutes = value; } 

       } 

       public BsCookie(string name,double expireMinutes) 

       { 

           _cookieName = name; 

           _expireMinutes = expireMinutes; 

       } 

       /// <summary> 

       /// 讀取對應的cookie 

       /// </summary> 

       /// <param name="name"></param> 

       /// <returns></returns> 

       private HttpCookie GetCookieReq() 

       { 

           HttpRequest request = HttpContext.Current.Request; 

           if (request != null) 

           { 

               HttpCookie cookie = request.Cookies[_cookieName]; 

               if (cookie != null) 

               { 

                   return cookie; 

               } 

           } 

           return null; 

       } 

       /// <summary> 

       /// 設置對應的cookie 

       /// </summary> 

       /// <param name="name"></param> 

       /// <returns></returns> 

       private HttpCookie GetCookieResponse() 

       { 

           HttpResponse response = HttpContext.Current.Response; 

           HttpCookie cookie = GetCookieReq(); 

           if (cookie != null) 

           { 

               string value = cookie.Value; 

               cookie = response.Cookies[_cookieName]; 

               cookie.Value = value; 

           } 

           else

           { 

               cookie = new HttpCookie(_cookieName); 

               cookie.HttpOnly = _httpOnly; 

               response.Cookies.Add(cookie); 

           } 

           if (Math.Abs(_expireMinutes) > 1 && cookie != null)//在存在的情況的下,延長過期日期 

           { 

               cookie.Expires = DateTime.Now.AddMinutes(_expireMinutes); 

           } 

           return cookie; 

       } 

       /// <summary> 

       /// 僅設置主鍵的 

       /// </summary> 

       /// <param name="name"></param> 

       /// <param name="value"></param> 

       /// <param name="exMinutes"></param> 

       public void SetCookie(string value) 

       { 

           _theCookie = GetCookieResponse(); 

           _theCookie.Value = HttpUtility.HtmlEncode(AllCommon.Encrypt(value)); 

           if (Math.Abs(_expireMinutes) > 1) 

           { 

               _theCookie.Expires = DateTime.Now.AddMinutes(_expireMinutes); 

           }            

       } 

       /// <summary> 

       /// 設置一組主鍵 

       /// </summary> 

       /// <param name="name"></param> 

       /// <param name="keys"></param> 

       /// <param name="exMinutes"></param> 

       public void SetCookie(Hashtable keys) 

       { 

           _theCookie = GetCookieResponse(); 

           foreach (DictionaryEntry de in keys) 

           { 

               _theCookie.Values[de.Key.ToString()] = HttpUtility.HtmlEncode(AllCommon.Encrypt(de.Value.ToString())); 

           } 

       } 

       /// <summary> 

       /// 獲取單一的cookie主鍵值 

       /// </summary> 

       /// <param name="name"></param> 

       /// <param name="exMinutes">需要延長的cookie的默認時間</param> 

       /// <returns></returns> 

       public string GetCookie() 

       { 

           _theCookie = GetCookieReq(); 

           if (_theCookie == null) 

           { 

               return string.Empty; 

           } 

           string thevalue = AllCommon.Decrypt(HttpUtility.HtmlDecode(_theCookie.Value)); 

           if (thevalue.Length > 0) 

           { 

               HttpCookie serverCookie = GetCookieResponse(); 

           } 

           return thevalue; 

       } 

       /// <summary> 

       /// 獲取一組對應的cookie值 

       /// </summary> 

       /// <param name="name"></param> 

       /// <param name="exMinutes"></param> 

       /// <returns></returns> 

       public Hashtable GetCookiesKeys() 

       { 

           _theCookie = GetCookieReq(); 

           if (_theCookie == null) 

           { 

               return null; 

           }             

           string[] keys = _theCookie.Values.AllKeys; 

           if (keys.Length > 0) 

           { 

               Hashtable keyHash = new Hashtable(); 

               foreach (string key in keys) 

               { 

                   keyHash.Add(key, AllCommon.Decrypt(HttpUtility.HtmlDecode(_theCookie.Values[key]))); 

               } 

               HttpCookie serverCookie = GetCookieResponse(); 

               return keyHash; 

           } 

           return null; 

       } 

       /// <summary> 

       /// 获取一组里面的单一个值 

       /// </summary> 

       /// <param name="keyname"></param> 

       /// <param name="exMinutes"></param> 

       /// <returns></returns> 

       public string GetCookieKV(string keyname) 

       { 

           _theCookie = GetCookieReq(); 

           if (_theCookie == null) 

           { 

               return string.Empty; 

           } 

           string result=AllCommon.Decrypt(HttpUtility.HtmlDecode(_theCookie.Values[keyname])); 

           if (result.Length > 0) 

           { 

               HttpCookie serverCookie = GetCookieResponse(); 

                 

           } 

           return result; 

       } 

       /// <summary> 

       /// 清除對應的cookie 

       /// </summary> 

       public void clearCookie() 

       { 

           HttpCookie serverCookie = GetCookieResponse(); 

           if (serverCookie != null) 

           { 

               serverCookie.Expires = DateTime.Now.AddDays(-1.0d); 

           } 

       } 

   }

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统