.net完美操作cookies
- using System;
- using System.Web;
-
- namespace Moosoft.OA.Public
- {
-
-
-
- public class CookiesHelper
- {
-
- #region 获取Cookie
-
-
-
-
-
- public static string GetCookieValue(string cookieName)
- {
- return GetCookieValue(cookieName, null);
- }
-
-
-
-
-
-
-
- public static string GetCookieValue(string cookieName, string key)
- {
- HttpRequest request = HttpContext.Current.Request;
- if (request != null)
- return GetCookieValue(request.Cookies[cookieName], key);
- return "";
- }
-
-
-
-
-
-
-
- public static string GetCookieValue(HttpCookie cookie, string key)
- {
- if (cookie != null)
- {
- if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
- return cookie.Values[key];
- else
- return cookie.Value;
- }
- return "";
- }
-
-
-
-
-
-
- public static HttpCookie GetCookie(string cookieName)
- {
- HttpRequest request = HttpContext.Current.Request;
- if (request != null)
- return request.Cookies[cookieName];
- return null;
- }
-
- #endregion
-
- #region 删除Cookie
-
-
-
-
-
- public static void RemoveCookie(string cookieName)
- {
- RemoveCookie(cookieName, null);
- }
-
-
-
-
-
-
- public static void RemoveCookie(string cookieName, string key)
- {
- HttpResponse response = HttpContext.Current.Response;
- if (response != null)
- {
- HttpCookie cookie = response.Cookies[cookieName];
- if (cookie != null)
- {
- if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
- cookie.Values.Remove(key);
- else
- response.Cookies.Remove(cookieName);
- }
- }
- }
-
- #endregion
-
- #region 设置/修改Cookie
-
-
-
-
-
-
-
- public static void SetCookie(string cookieName, string key, string value)
- {
- SetCookie(cookieName, key, value, null);
- }
-
-
-
-
-
-
- public static void SetCookie(string key, string value)
- {
- SetCookie(key, null, value, null);
- }
-
-
-
-
-
-
-
- public static void SetCookie(string key, string value, DateTime expires)
- {
- SetCookie(key, null, value, expires);
- }
-
-
-
-
-
-
- public static void SetCookie(string cookieName, DateTime expires)
- {
- SetCookie(cookieName, null, null, expires);
- }
-
-
-
-
-
-
-
-
- public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
- {
- HttpResponse response = HttpContext.Current.Response;
- if (response != null)
- {
- HttpCookie cookie = response.Cookies[cookieName];
- if (cookie != null)
- {
- if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
- cookie.Values.Set(key, value);
- else
- if (!string.IsNullOrEmpty(value))
- cookie.Value = value;
- if (expires != null)
- cookie.Expires = expires.Value;
- response.SetCookie(cookie);
- }
- }
-
- }
-
- #endregion
-
- #region 添加Cookie
-
-
-
-
-
-
- public static void AddCookie(string key, string value)
- {
- AddCookie(new HttpCookie(key, value));
- }
-
-
-
-
-
-
-
- public static void AddCookie(string key, string value, DateTime expires)
- {
- HttpCookie cookie = new HttpCookie(key, value);
- cookie.Expires = expires;
- AddCookie(cookie);
- }
-
-
-
-
-
-
-
- public static void AddCookie(string cookieName, string key, string value)
- {
- HttpCookie cookie = new HttpCookie(cookieName);
- cookie.Values.Add(key, value);
- AddCookie(cookie);
- }
-
-
-
-
-
-
- public static void AddCookie(string cookieName, DateTime expires)
- {
- HttpCookie cookie = new HttpCookie(cookieName);
- cookie.Expires = expires;
- AddCookie(cookie);
- }
-
-
-
-
-
-
-
-
- public static void AddCookie(string cookieName, string key, string value, DateTime expires)
- {
- HttpCookie cookie = new HttpCookie(cookieName);
- cookie.Expires = expires;
- cookie.Values.Add(key, value);
- AddCookie(cookie);
- }
-
-
-
-
-
- public static void AddCookie(HttpCookie cookie)
- {
- HttpResponse response = HttpContext.Current.Response;
- if (response != null)
- {
-
- cookie.HttpOnly = true;
-
- cookie.Path = "/";
-
-
- response.AppendCookie(cookie);
- }
- }
-
- #endregion
- }
- }