using System; using System.Web; using XTSoft.Com.DwCmsException; namespace XTSoft.Com.Common { public sealed class CookieHelper { private CookieHelper() { } /// <summary> /// 获取一个数组形式的Cookies /// </summary> public static HttpCookie GetCookie(string name) { if (!string.IsNullOrEmpty(name)) { return HttpContext.Current.Request.Cookies[name]; } else { throw new DwException("键值为空!"); } } /// <summary> /// 移除Cookies /// </summary> public static void RemoveCookie(string name) { RemoveCookie(GetCookie(name)); } /// <summary> /// 移除Cookies /// </summary> public static void RemoveCookie(HttpCookie cookie) { if (cookie != null) { cookie.Expires = new DateTime(1983, 1, 2); Save(cookie); } } /// <summary> /// 保存Cookies /// </summary> public static void Save(HttpCookie cookie) { cookie.Domain = ""; HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// 获取一个新的Cookies /// </summary> public static HttpCookie GetNewCookie(string name) { return new HttpCookie(name); } /// <summary> /// 取得指定名称的单值Cookie /// </summary> /// <returns></returns> public static string GetCookieValue(string name) { HttpCookie cookie = GetCookie(name); if (cookie == null || cookie.Value == null) { return string.Empty; } else { return cookie.Value; } } /// <summary> /// 保存指定名称的单值Cookie /// </summary> /// <returns></returns> public static void SetCookie(string name, string value) { SetCookie(name, value, DateTime.Now); } /// <summary> /// 保存指定名称的单值Cookie /// </summary> /// <returns></returns> public static void SetCookie(string name, string value, DateTime expires) { HttpCookie cookie = GetCookie(name); if (cookie == null) { cookie = GetNewCookie(name); } cookie.Value = value; cookie.Expires = expires; Save(cookie); } /// <summary> /// 保存指定名称的单值Cookie /// </summary> /// <returns></returns> public static void SetCookie(string name, string value, string path, DateTime expires) { HttpCookie cookie = GetCookie(name); if (cookie == null) { cookie = GetNewCookie(name); } cookie.Path = path; cookie.Value = value; cookie.Expires = expires; Save(cookie); } /// <summary> /// 保存指定名称的单值Cookie /// </summary> /// <returns></returns> public static void SetCookie(string name, string value, string path) { SetCookie(name, value, path, DateTime.Now); } } }