/// <summary> /// Cookie操作 /// </summary> public class Cookie { private Cookie() { }
private static Cookie _instance = new Cookie();
public static Cookie _ { get { return _instance; } set { _instance = value; } }
/// <summary> /// 添加Cookie /// </summary> /// <param name="key">鍵名</param> /// <param name="value">鍵值</param> /// <param name="hour">保存時(shí)間(小時(shí))</param> public void SetCookie(string key, string value, string path, string domain, DateTime expires) { HttpCookie hc = new HttpCookie(key); hc.Path = path; hc.Domain = domain; hc.Value = HttpUtility.UrlEncode(value); hc.Expires = expires;
HttpContext.Current.Response.Cookies.Add(hc); }
/// <summary> /// 添加Cookie /// </summary> /// <param name="key">鍵名</param> /// <param name="value">鍵值</param> /// <param name="hour">保存時(shí)間(小時(shí))</param> public void SetCookie(string key, string value, string path, string domain, int hour) { SetCookie(key, value, path, domain, DateTime.Now.AddHours(hour)); }
/// <summary> /// 添加Cookie /// </summary> /// <param name="key">鍵名</param> /// <param name="value">鍵值</param> public void SetCookie(string key, string value, string path, string domain) { SetCookie(key, value, path, domain, 0); }
/// <summary> /// 讀取Cookie /// </summary> public string GetCookieByKey(string key) { HttpCookie cookie = HttpContext.Current.Request.Cookies[key]; return cookie != null ? HttpUtility.UrlDecode(cookie.Value) : ""; }
public void ClearCookieByKey(string key, string path, string domain) { HttpCookie hc = new HttpCookie(key); hc.Path = path; hc.Domain = domain; hc.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(hc); } }
|