点燃自己

才发现已成尘

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  • #region Cookie的操作:Cookie的读取、Cookie的保存    
  •     /// <summary>    
  •     /// 静态方法:保存Cookie    
  •     /// </summary>    
  •     /// <param name="_name">名称</param>    
  •     /// <param name="_value">参数</param>    
  •     public static void SaveCookie(Hashtable Hash, int _remberme, string _CookieName)    
  •     {    
  •         string DoMain = "";    
  •         string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');    
  •         for (int i = 1; i < Arr.Length; i++)    
  •         {    
  •             DoMain += "." + Arr[i];    
  •         }    
  •         if (_CookieName == "")    
  •         {    
  •             _CookieName = ConfigurationManager.AppSettings["CookieName"];    
  •         }    
  •         HttpCookie Cookie = new HttpCookie(_CookieName);    
  •         foreach (DictionaryEntry entry in Hash)    
  •         {    
  •             Cookie[entry.Key.ToString()] = entry.Value.ToString();    
  •         }    
  •         //Cookie.Domain = ConfigurationManager.AppSettings["CookieDoMain"];    
  •         if (_remberme == 1)    
  •         {    
  •             Cookie.Expires=DateTime.UtcNow.AddDays(30);    
  •         }    
  •         else    
  •         {    
  •             Cookie.Expires=DateTime.UtcNow.AddDays(1);    
  •         }    
  •         Cookie.Domain = DoMain;    
  •         HttpContext.Current.Response.Cookies.Add(Cookie);    
  •     }    
  •     
  •     /// <summary>    
  •     /// 静态方法:读取Cookie的值    
  •     /// </summary>    
  •     /// <param name="_name"></param>    
  •     /// <returns></returns>    
  •     public static string GetCookie(string _name)    
  •     {    
  •         HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(ConfigurationManager.AppSettings["CookieName"]);    
  •         if (Cookie != null)    
  •         {    
  •             string DoMain = "";    
  •             string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');    
  •             for (int i = 1; i < Arr.Length; i++)    
  •             {    
  •                 DoMain += "." + Arr[i];    
  •             }    
  •             Cookie.Domain = DoMain;    
  •             return Cookie[_name];    
  •         }    
  •         else    
  •         {    
  •             return "";    
  •         }    
  •     
  •     }    
  •     /// <summary>    
  •     /// 静态方法:读取Cookie的值    
  •     /// </summary>    
  •     /// <param name="_name"></param>    
  •     /// <returns></returns>    
  •     public static string GetCookie(string _name,string _CookieName)    
  •     {    
  •         if (_CookieName == "")    
  •         {    
  •             _CookieName = ConfigurationManager.AppSettings["CookieName"];    
  •         }    
  •         HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(_CookieName);    
  •         if (Cookie != null)    
  •         {    
  •             string DoMain = "";    
  •             string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');    
  •             for (int i = 1; i < Arr.Length; i++)    
  •             {    
  •                 DoMain += "." + Arr[i];    
  •             }    
  •             Cookie.Domain = DoMain;    
  •             return Cookie[_name];    
  •         }    
  •         else    
  •         {    
  •             return "";    
  •         }    
  •     
  •     }   
  •     #endregion  
posted on 2014-11-27 11:03  爱吃糖豆的猪  阅读(250)  评论(0)    收藏  举报