ASP.NET-----Cookie静的操作類

12049 ワード

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web;

using System.Collections.Specialized;



namespace Core.Common.Web

{

    /// <summary>

    /// Cookie     

    /// </summary>

    public static class Cookie

    {

        #region     



        /// <summary>

        ///      COOKIE    Value 

        /// </summary>

        /// <param name="strCookieName">COOKIE   </param>

        /// <param name="strValue">COOKIE  Value </param>

        /// <remarks>

        ///  COOKIE       Expires

        /// </remarks>

        public static void SetObject(string strCookieName, string strValue)

        {

            SetObject(strCookieName, 1, strValue);

        }



        /// <summary>

        ///      COOKIE    Value 

        /// </summary>

        /// <param name="strCookieName">COOKIE   </param>

        /// <param name="iExpires">

        /// COOKIE      (  )

        /// 1      ,0            

        ///     2        

        /// 86400  = 1  = (60*60*24)

        /// 604800  = 1  = (60*60*24*7)

        /// 2593000  = 1  = (60*60*24*30)

        /// 31536000  = 1  = (60*60*24*365)

        /// </param>

        /// <param name="strValue">COOKIE  Value </param>

        /// <remarks>

        ///  COOKIE       Expires

        /// </remarks>

        public static void SetObject(string strCookieName, int iExpires, string strValue)

        {

            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());

            objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim());

            if (iExpires > 0)

            {

                if (iExpires == 1)

                {

                    objCookie.Expires = DateTime.MaxValue;

                }

                else

                {

                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);

                }

            }

            HttpContext.Current.Response.Cookies.Add(objCookie);

        }



        /// <summary>

        ///   COOKIE      KEY  

        /// </summary>

        /// <param name="strCookieName">COOKIE   </param>

        /// <param name="iExpires">

        /// COOKIE      (  )

        /// 1      ,0            

        ///     2        

        /// 86400  = 1  = (60*60*24)

        /// 604800  = 1  = (60*60*24*7)

        /// 2593000  = 1  = (60*60*24*30)

        /// 31536000  = 1  = (60*60*24*365)

        /// </param>

        /// <param name="KeyValue"> /    </param>

        public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue)

        {

            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());

            foreach (string key in KeyValue.AllKeys)

            {

                objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim());

            }

            if (iExpires > 0)

            {

                if (iExpires == 1)

                {

                    objCookie.Expires = DateTime.MaxValue;

                }

                else

                {

                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);

                }

            }

            HttpContext.Current.Response.Cookies.Add(objCookie);

        }



        /// <summary>

        ///      COOKIE    Value 

        /// </summary>

        /// <param name="strCookieName">COOKIE   </param>

        /// <param name="iExpires">

        /// COOKIE      (  )

        /// 1      ,0            

        ///     2        

        /// 86400  = 1  = (60*60*24)

        /// 604800  = 1  = (60*60*24*7)

        /// 2593000  = 1  = (60*60*24*30)

        /// 31536000  = 1  = (60*60*24*365)

        /// </param>

        /// <param name="strDomain">   </param>

        /// <param name="strValue">COOKIE  Value </param>

        /// <remarks>

        ///  COOKIE       Expires

        /// </remarks>

        public static void SetObject(string strCookieName, int iExpires, string strValue, string strDomain)

        {

            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());

            objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim());

            objCookie.Domain = strDomain.Trim();

            if (iExpires > 0)

            {

                if (iExpires == 1)

                {

                    objCookie.Expires = DateTime.MaxValue;

                }

                else

                {

                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);

                }

            }

            HttpContext.Current.Response.Cookies.Add(objCookie);

        }



        /// <summary>

        ///   COOKIE      KEY  

        /// </summary>

        /// <param name="strCookieName">COOKIE   </param>

        /// <param name="iExpires">

        /// COOKIE      (  )

        /// 1      ,0            

        ///     2        

        /// 86400  = 1  = (60*60*24)

        /// 604800  = 1  = (60*60*24*7)

        /// 2593000  = 1  = (60*60*24*30)

        /// 31536000  = 1  = (60*60*24*365)

        /// </param>

        /// <param name="strDomain">   </param>

        /// <param name="KeyValue"> /    </param>

        public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomain)

        {

            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());

            foreach (string key in KeyValue.AllKeys)

            {

                objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim());

            }

            objCookie.Domain = strDomain.Trim();

            if (iExpires > 0)

            {

                if (iExpires == 1)

                {

                    objCookie.Expires = DateTime.MaxValue;

                }

                else

                {

                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);

                }

            }

            HttpContext.Current.Response.Cookies.Add(objCookie);

        }



        /// <summary>

        ///   Cookie     Value ,  Value 

        /// </summary>

        /// <param name="strCookieName">Cookie    </param>

        /// <returns>Value ,         ,    null</returns>

        public static string GetValue(string strCookieName)

        {

            if (HttpContext.Current.Request.Cookies[strCookieName] == null)

            {

                return null;

            }

            else

            {

                return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName].Value);

            }

        }



        /// <summary>

        ///   Cookie       Key    

        /// </summary>

        /// <param name="strCookieName">Cookie    </param>

        /// <param name="strKeyName">Key  </param>

        /// <returns>Key  ,           ,    null</returns>

        public static string GetValue(string strCookieName, string strKeyName)

        {

            if (HttpContext.Current.Request.Cookies[strCookieName] == null)

            {

                return null;

            }

            else

            {

                string strObjValue = HttpContext.Current.Request.Cookies[strCookieName].Value;

                string strKeyName2 = strKeyName + "=";

                if (strObjValue.IndexOf(strKeyName2) == -1)

                {

                    return null;

                }

                else

                {

                    return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName][strKeyName]);

                }

            }

        }



        /// <summary>

        ///     COOKIE    Key        COOKIE    Key 

        /// </summary>

        /// <param name="strCookieName">Cookie    </param>

        /// <param name="strKeyName">Key  </param>

        /// <param name="KeyValue">Key  </param>

        /// <param name="iExpires">

        /// COOKIE      (  )

        /// 1      ,0            

        ///     2        

        /// 86400  = 1  = (60*60*24)

        /// 604800  = 1  = (60*60*24*7)

        /// 2593000  = 1  = (60*60*24*30)

        /// 31536000  = 1  = (60*60*24*365)

        /// </param>

        /// <returns>         ,    false</returns>

        public static bool Edit(string strCookieName, string strKeyName, string KeyValue, int iExpires)

        {

            if (HttpContext.Current.Request.Cookies[strCookieName] == null)

            {

                return false;

            }

            else

            {

                HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName];

                objCookie[strKeyName] = HttpContext.Current.Server.UrlEncode(KeyValue.Trim());

                if (iExpires > 0)

                {

                    if (iExpires == 1)

                    {

                        objCookie.Expires = DateTime.MaxValue;

                    }

                    else

                    {

                        objCookie.Expires = DateTime.Now.AddSeconds(iExpires);

                    }

                }

                HttpContext.Current.Response.Cookies.Add(objCookie);

                return true;

            }

        }



        /// <summary>

        ///   COOKIE  

        /// </summary>

        /// <param name="strCookieName">Cookie    </param>

        public static void Delete(string strCookieName)

        {

            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());

            objCookie.Expires = DateTime.Now.AddYears(-5);

            HttpContext.Current.Response.Cookies.Add(objCookie);

        }



        /// <summary>

        ///     COOKIE    Key  

        /// </summary>

        /// <param name="strCookieName">Cookie    </param>

        /// <param name="strKeyName">Key  </param>

        /// <param name="iExpires">

        /// COOKIE      (  )

        /// 1      ,0            

        ///     2        

        /// 86400  = 1  = (60*60*24)

        /// 604800  = 1  = (60*60*24*7)

        /// 2593000  = 1  = (60*60*24*30)

        /// 31536000  = 1  = (60*60*24*365)

        /// </param>

        /// <returns>         ,    false</returns>

        public static bool Delete(string strCookieName, string strKeyName, int iExpires)

        {

            if (HttpContext.Current.Request.Cookies[strCookieName] == null)

            {

                return false;

            }

            else

            {

                HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName];

                objCookie.Values.Remove(strKeyName);

                if (iExpires > 0)

                {

                    if (iExpires == 1)

                    {

                        objCookie.Expires = DateTime.MaxValue;

                    }

                    else

                    {

                        objCookie.Expires = DateTime.Now.AddSeconds(iExpires);

                    }

                }

                HttpContext.Current.Response.Cookies.Add(objCookie);

                return true;

            }

        }



        #endregion

    }

}