1. 程式人生 > >C# cookie寫入和獲取

C# cookie寫入和獲取

C# cookie寫入和獲取

#region 讀取或寫入cookie

        ///

        /// 寫cookie值

        ///

        /// 名稱

        /// 值

        public static void WriteCookie(string strName, string strValue)

        {

            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];

            if (cookie == null)

            {

                cookie = new HttpCookie(strName);

            }

            cookie.Value = UrlEncode(strValue);

            HttpContext.Current.Response.AppendCookie(cookie);

        }

 

        ///

        /// 寫cookie值

        ///

        /// 名稱

        /// 值

        public static void WriteCookie(string strName, string key, string strValue)

        {

            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];

            if (cookie == null)

            {

                cookie = new HttpCookie(strName);

            }

            cookie[key] = UrlEncode(strValue);

            HttpContext.Current.Response.AppendCookie(cookie);

        }

 

        ///

        /// 寫cookie值

        ///

        /// 名稱

        /// 值

        public static void WriteCookie(string strName, string key, string strValue, int expires)

        {

            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];

            if (cookie == null)

            {

                cookie = new HttpCookie(strName);

            }

            cookie[key] = UrlEncode(strValue);

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

            HttpContext.Current.Response.AppendCookie(cookie);

        }

 

        ///

        /// 寫cookie值

        ///

        /// 名稱

        /// 值

        /// 過期時間(分鐘)

        public static void WriteCookie(string strName, string strValue, int expires)

        {

            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];

            if (cookie == null)

            {

                cookie = new HttpCookie(strName);

            }

            cookie.Value = UrlEncode(strValue);

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

            HttpContext.Current.Response.AppendCookie(cookie);

        }

 

        ///

        /// 讀cookie值

        ///

        /// 名稱

        /// cookie值

        public static string GetCookie(string strName)

        {

            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)

                return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());

            return "";

        }

 

        ///

        /// 讀cookie值

        ///

        /// 名稱

        /// cookie值

        public static string GetCookie(string strName, string key)

        {

            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)

                return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());

 

            return "";

        }

        #endregion