using System;
using System.Web;


namespace Common
{
    public class CookieHelper
    {
        /// <summary>
        /// 设置Cookie缓存
        /// </summary>
        /// <param name="sKey"></param>
        /// <param name="sContent"></param>
        /// <param name="dt"></param>
        public static void CookieSet(string sKey,string sContent,DateTime dt)
        {
            HttpCookie myHttpCookie = new HttpCookie(sKey);
            myHttpCookie.Value = sContent;
            myHttpCookie.Expires = dt;
            HttpContext.Current.Response.Cookies.Add(myHttpCookie);
        }


        /// <summary>  
        /// 清除指定Cookie  
        /// </summary>  
        /// <param name="sKey">cookiename</param>  
        public static void CookieClear(string sKey)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[sKey];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddYears(-3);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>  
        /// 获取指定Cookie值  
        /// </summary>  
        /// <param name="sKey">cookiename</param>  
        /// <returns></returns>  
        public static string CookieGet(string sKey)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[sKey];
            string str = string.Empty;
            if (cookie != null)
            {
                str = cookie.Value;
            }
            return str;
        }
    }
}


posted on 2017-02-04 15:48  cxd1008  阅读(168)  评论(0编辑  收藏  举报