Cookie操作公共方法类

using System;
using System.Web;

namespace AmwITx.Framework.Web.Components
{
    /// <summary>
    /// Cookie
    /// </summary>
    public class Cookie
    {
        private Cookie() { }

        private static Cookie _instance = new Cookie();

        public static Cookie Instance
        {
            get { return _instance; }
            set { _instance = value; }
        }

        /// <summary>
        /// SetCookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Set(string key, string value)
        {
            Set(key, value, "/", "", 0);
        }

        /// <summary>
        /// SetCookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="path"></param>
        /// <param name="domain">二级域名时定位</param>
        /// <param name="hour"></param>
        public void Set(string key, string value, string path, string domain, int hour)
        {
            Set(key, value, path, domain, DateTime.Now.AddHours(hour));
        }

        /// <summary>
        /// SetCookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="path"></param>
        /// <param name="domain">二级域名时定位</param>
        /// <param name="expires"></param>
        public void Set(string key, string value, string path, string domain, DateTime expires)
        {
            var cookie = HttpContext.Current.Request.Cookies[key];
            bool isAdd = false;
            if (cookie == null)
            {
                isAdd = true;
                cookie = new HttpCookie(key);
            }
            cookie.Path = path;
            cookie.Domain = domain;
            cookie.Value = HttpUtility.UrlEncode(value);
            if (expires > DateTime.Now)
            {
                cookie.Expires = expires;
            }

            if (isAdd)
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }

        /// <summary>
        /// Cookie
        /// </summary>
        public string Get(string key)
        {
            var cookie = HttpContext.Current.Request.Cookies[key];
            return cookie != null ? HttpUtility.UrlDecode(cookie.Value) : "";
        }
        /// <summary>
        /// Cookie
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            Remove(key, "", "");
        }

        public void Remove(string key, string path, string domain)
        {
            var hc = new HttpCookie(key);
            if (path != "")
            {
                hc.Path = path;
            }
            if (domain != "")
            {
                hc.Domain = domain;
            }
            hc.Expires = DateTime.Now.AddDays(-1);

            HttpContext.Current.Response.Cookies.Add(hc);
        }
    }
}
posted @ 2010-07-15 16:50  黄靖洆  阅读(774)  评论(0编辑  收藏  举报