asp.net 조작 쿠키 클래스, 데이터테이블 대량 저장 쿠키 포함

17993 단어 Datatable
클래스는 다음과 같습니다.
   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(이름, 이름);

좋은 웹페이지 즐겨찾기