分類:
Asp.Net
2009-08-03 11:30
523人閱讀
收藏
舉報(bào)
- public class CookieHelper
- {
- #region 為Cookie賦值方法
-
-
-
-
-
-
-
-
-
- public static bool WriteCookie(string CookieName, NameValueCollection Nvc, int days, string Domain)
- {
- bool BoolReturnValue = false;
- if (Nvc != null && !string.IsNullOrEmpty(CookieName))
- {
- HttpCookie httpCookie = new HttpCookie(CookieName);
- for (int i = 0; i < Nvc.Count; i++)
- {
- httpCookie.Values.Add(Nvc.GetKey(i),HttpUtility.UrlEncode(Nvc.Get(i)));
- }
- if (days > 0)
- {
- httpCookie.Expires = DateTime.Now.AddDays(days);
- }
-
- if (!string.IsNullOrEmpty(Domain))
- {
- httpCookie.Domain = Domain;
- }
- HttpContext.Current.Response.AppendCookie(httpCookie);
- BoolReturnValue = true;
- }
- return BoolReturnValue;
- }
-
-
-
-
-
-
- public static bool WriteCookieNoDay(string CookieName, NameValueCollection Nvc)
- {
- bool BoolReturnValue = false;
- if (Nvc != null && !string.IsNullOrEmpty(CookieName))
- {
- HttpCookie httpCookie = new HttpCookie(CookieName);
- for (int i = 0; i < Nvc.Count; i++)
- {
- httpCookie.Values.Add(Nvc.GetKey(i), Nvc.Get(i));
- }
- HttpContext.Current.Response.AppendCookie(httpCookie);
- BoolReturnValue = true;
- }
- return BoolReturnValue;
- }
-
-
-
-
-
-
-
-
- public static bool WriteCookie(string CookieName, NameValueCollection Nvc, int days)
- {
- return WriteCookie(CookieName, Nvc, days, null);
- }
-
-
-
-
-
-
-
- public static bool WriteCookie(string CookieName, NameValueCollection Nvc)
- {
- return WriteCookie(CookieName, Nvc, 1, null);
- }
- #endregion
- #region 添加Cookie值
-
-
-
-
-
-
- public static bool AddCookie(string CookieName, NameValueCollection Nvc)
- {
- bool BoolReturnValue = false;
- if (Nvc != null && !string.IsNullOrEmpty(CookieName))
- {
- for (int i = 0; i < Nvc.Count; i++)
- {
- HttpContext.Current.Request.Cookies[CookieName].Values.Add(Nvc.GetKey(i), Nvc.Get(i));
- HttpContext.Current.Response.Cookies[CookieName].Values.Add(Nvc.GetKey(i), Nvc.Get(i));
- }
- }
- return BoolReturnValue;
- }
- #endregion
- #region 更新Cookie
-
-
-
-
-
-
-
- public static bool UpdateCookie(string CookieName, NameValueCollection Nvc)
- {
- bool BoolReturnValue = false;
- if (Nvc != null && !string.IsNullOrEmpty(CookieName))
- {
- HttpCookie httpCookie = new HttpCookie(CookieName);
- NameValueCollection NonceNvc = HttpContext.Current.Request.Cookies[CookieName].Values;
- if (NonceNvc != null)
- {
- string CookieValue = string.Empty;
-
- for (int i = 0; i < NonceNvc.Count; i++)
- {
- CookieValue = NonceNvc.Get(i);
- for (int y = 0; y < Nvc.Count; y++)
- {
- if (NonceNvc.GetKey(i) == Nvc.GetKey(y))
- {
- if (CookieValue != Nvc.Get(y))
- {
- CookieValue = Nvc.Get(y);
- }
- break;
- }
- }
- httpCookie.Values.Add(NonceNvc.GetKey(i), CookieValue);
- CookieValue = string.Empty;
- }
-
- HttpContext.Current.Response.AppendCookie(httpCookie);
- BoolReturnValue = true;
- }
- }
- return BoolReturnValue;
- }
- #endregion
- #region 得到Cookie值集合
-
-
-
-
-
-
- public static NameValueCollection GetCookie(string CookieName)
- {
- NameValueCollection Nvc = new NameValueCollection();
- if (!string.IsNullOrEmpty(CookieName) && HttpContext.Current.Request.Cookies[CookieName] != null)
- {
- Nvc = HttpContext.Current.Request.Cookies[CookieName].Values;
- }
- return Nvc;
- }
- #endregion
- #region 刪除Cookie
-
-
-
-
-
-
- public static bool DeleteCookie(string CookieName, string CookieDomain)
- {
- bool BoolReturnValue = false;
- HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookieName];
- if (httpCookie != null)
- {
- if (!string.IsNullOrEmpty(CookieDomain))
- {
- httpCookie.Domain = CookieDomain;
- }
- httpCookie.Expires = DateTime.Now.AddDays(-30);
- HttpContext.Current.Response.Cookies.Add(httpCookie);
- BoolReturnValue = true;
- }
- return BoolReturnValue;
- }
-
-
-
-
-
-
- public static bool DeleteCookie(string CookieName)
- {
- return DeleteCookie(CookieName, null);
- }
- #endregion
- #region 得到單獨(dú)一條Cookie的值
-
-
-
-
-
-
-
- public static string GetSingleValue(string CookieName, string KeyName)
- {
- string StrReturnValue = string.Empty;
- HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookieName];
- if (httpCookie != null)
- {
- StrReturnValue =HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName].Values[KeyName]);
- }
- return StrReturnValue;
- }
-
-
-
-
-
-
-
- public static string GetSingleValueFromServer(string CookieName, string KeyName)
- {
- string StrReturnValue = string.Empty;
- HttpCookie httpCookie = HttpContext.Current.Response.Cookies[CookieName];
- if (httpCookie != null)
- {
- StrReturnValue = HttpContext.Current.Response.Cookies[CookieName].Values[KeyName];
- }
- return StrReturnValue;
- }
-
- #endregion
- #region 更新單獨(dú)一條Cookie的值
-
-
-
-
-
-
-
-
- public static bool UpdateSingleValue(string CookieName,string KeyName, string Value)
- {
- bool BoolReturnValue = false;
- NameValueCollection nvc = new NameValueCollection();
- nvc.Add(KeyName, Value);
- if (!string.IsNullOrEmpty(CookieName))
- {
- HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookieName];
- if (httpCookie != null)
- {
- if (HttpContext.Current.Request.Cookies[CookieName].Values[KeyName] != null)
- {
- BoolReturnValue = UpdateCookie(CookieName, nvc);
- }
- else
- {
- BoolReturnValue = AddCookie(CookieName, nvc);
- }
- }
- else
- {
- BoolReturnValue = WriteCookie(CookieName, nvc);
- }
- }
- return BoolReturnValue;
- }
- #endregion
- #region 添加單獨(dú)的一條Cookie值
-
-
-
-
-
-
-
- public static bool AddSingleCookie(string CookieName, string KeyName, string Value)
- {
- bool BoolReturnValue = false;
- NameValueCollection nvc = new NameValueCollection();
- nvc.Add(KeyName, Value);
- if (!string.IsNullOrEmpty(CookieName))
- {
- HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookieName];
- if (httpCookie != null)
- {
- if (HttpContext.Current.Request.Cookies[CookieName].Values[KeyName] != null)
- {
- BoolReturnValue = UpdateCookie(CookieName, nvc);
- }
- else
- {
- BoolReturnValue = AddCookie(CookieName, nvc);
- }
- }
- else
- {
- BoolReturnValue = WriteCookie(CookieName, nvc);
- }
- }
- return BoolReturnValue;
- }
- #endregion
- #region 判斷是否存在Cookie表
-
-
-
-
-
- public static bool HasCookie(string CookieName)
- {
- bool BoolReturnValue = false;
- if (HttpContext.Current.Request.Cookies[CookieName] != null)
- BoolReturnValue = true;
- return BoolReturnValue;
- }
- #endregion
- }
|