ASP.NET 對cookies的操作

作者:互联网   出处:控件中国网   2014-11-05 19:05:44   阅读:1

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); 

           } 

       } 

   }

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat