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; } } }
本文为博主原创文章,欢迎转载,但转载须注在明显位置注明【博客地址】和【原文地址】,否则将追究法律责任。http://www.cnblogs.com/cxd1008