asp.Net操作クッキークラス、datatable一括格納クッキーを含む

17990 ワード

以下はクラスです.
   public class CookieMgr {
        #region     Cookie
        /// <summary>
        ///     Cookie
        /// </summary>
        /// <param name="dt">    </param>
        /// <param name="cookieName">Cookie  </param>
        /// <param name="day"></param>
        public static void SaveCookie(DataTable dt, string cookieName, int day) {
            HttpCookie cookie = new HttpCookie(cookieName);
            if (day != 0)
                cookie.Expires = DateTime.Now.AddDays(day);

            for (int i = 0; i < dt.Columns.Count; i++) {
                cookie.Values.Add(dt.Columns[i].ColumnName, StringMgr.Encrypt(dt.Rows[0][dt.Columns[i].ColumnName].ToString())); //  
            }
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        #endregion

        #region      Cookie
        /// <summary>
        ///      Cookie
        /// </summary>
        /// <param name="cookieName">cookie  </param>
        /// <param name="day"></param>
        public static void InitCookie(string cookieName, int day) {
            HttpCookie cookie = new HttpCookie(cookieName);
            if (day != 0)
                cookie.Expires = DateTime.Now.AddDays(day);

            cookie.Values.Add("initExpires", StringMgr.Encrypt(day.ToString()));
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion

        #region   Cookie      ,             
        /// <summary>
        ///     Cookie  ,              
        /// </summary>
        /// <param name="cookieName">Cookie  </param>
        /// <param name="key">     </param>
        /// <param name="value">     </param>
        public static void SetCookie(string cookieName, string key, string value) {
            HttpCookie cookie = null;
            if (HttpContext.Current.Request.Cookies[cookieName] != null) {
                cookie = HttpContext.Current.Request.Cookies[cookieName];
            } else
                cookie = new HttpCookie(cookieName);

            bool b = false;
            for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                if (cookie.Values.Keys[i] == key) {
                    b = true;
                    break;
                }
            }
            value = StringMgr.Encrypt(value);
            if (b)
                cookie.Values[key] = value;
            else
                cookie.Values.Add(key, value);


            for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                if (cookie.Values.Keys[i] == "initExpires") {
                    cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString())));
                    break;
                }
            }


            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion

        #region   Cookie      
        /// <summary>
        ///   Cookie      
        /// </summary>
        /// <param name="cookieName">Cookie  </param>
        /// <param name="key">     </param>
        /// <returns>    【CookieName】  【key】  </returns>
        public static string GetCookie(string cookieName, string key) {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null && cookie.Values[key] != null)
                return StringMgr.Decrypt(cookie.Values[key].ToString());
            else
                return Param.CookieErrorString;
        }
        #endregion

        #region   Cookie      
        /// <summary>
        ///   Cookie      
        /// </summary>
        /// <param name="cookieName">Cookie  </param>
        /// <param name="key">     </param>
        public static void RemoveCookie(string cookieName, string key) {
            HttpCookie cookie = null;
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
                cookie = HttpContext.Current.Request.Cookies[cookieName];
            else
                cookie = new HttpCookie(cookieName);

            bool b = false;
            for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                if (cookie.Values.Keys[i] == key) {
                    b = true;
                    break;
                }
            }
            if (b)
                cookie.Values.Remove(key);

            for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                if (cookie.Values.Keys[i] == "initExpires") {
                    cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString())));
                    break;
                }
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion

        #region   Cookie
        /// <summary>
        ///   Cookie
        /// </summary>
        /// <param name="cookieName">Cookie  </param>
        public static void ClearCookie(string cookieName) {
            System.Web.HttpCookie cookie = new System.Web.HttpCookie(cookieName);
            cookie.Expires = System.DateTime.Now.AddYears(-1);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        #endregion

        #region   Cookie   
        /// <summary>
        ///   Cookie   
        /// </summary>
        /// <param name="cookieName">Cookie  </param>
        /// <returns>  Cookie   </returns>
        public static NameValueCollection GetCookieValues(string cookieName) {
            if (HttpContext.Current.Request.Cookies[cookieName] != null) {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                return cookie.Values;
            } else
                return null;
        }
        #endregion
    }

DTをクッキーに保存
ookieMgr.SaveCookie(dt,「名称」,0);
クッキーキー値の取得
CookieMgr.GetCookie(「名前」、「名前」);