程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net下cookies操作完美代碼

asp.net下cookies操作完美代碼

編輯:ASP.NET基礎
復制代碼 代碼如下:
using System;
using System.Web;
namespace Moosoft.OA.Public
{
/// <summary>
/// Cookie幫助類
/// </summary>
public class CookiesHelper
{
#region 獲取Cookie
/// <summary>
/// 獲得Cookie的值
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName)
{
return GetCookieValue(cookieName, null);
}
/// <summary>
/// 獲得Cookie的值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName, string key)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return GetCookieValue(request.Cookies[cookieName], key);
return "";
}
/// <summary>
/// 獲得Cookie的子鍵值
/// </summary>
/// <param name="cookie"></param>
/// <param name="key"></param>
/// <returns></returns>
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 "";
}
/// <summary>
/// 獲得Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static HttpCookie GetCookie(string cookieName)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return null;
}
#endregion
#region 刪除Cookie
/// <summary>
/// 刪除Cookie
/// </summary>
/// <param name="cookieName"></param>
public static void RemoveCookie(string cookieName)
{
RemoveCookie(cookieName, null);
}
/// <summary>
/// 刪除Cookie的子鍵
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
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
/// <summary>
/// 設置Cookie子鍵的值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string cookieName, string key, string value)
{
SetCookie(cookieName, key, value, null);
}
/// <summary>
/// 設置Cookie值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string key, string value)
{
SetCookie(key, null, value, null);
}
/// <summary>
/// 設置Cookie值和過期時間
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void SetCookie(string key, string value, DateTime expires)
{
SetCookie(key, null, value, expires);
}
/// <summary>
/// 設置Cookie過期時間
/// </summary>
/// <param name="cookieName"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName, DateTime expires)
{
SetCookie(cookieName, null, null, expires);
}
/// <summary>
/// 設置Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
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
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddCookie(string key, string value)
{
AddCookie(new HttpCookie(key, value));
}
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void AddCookie(string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(key, value);
cookie.Expires = expires;
AddCookie(cookie);
}
/// <summary>
/// 添加為Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddCookie(string cookieName, string key, string value)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Values.Add(key, value);
AddCookie(cookie);
}
/// <summary>
/// 添加為Cookie集合
/// </summary>
/// <param name="cookieName">Cookie名稱</param>
/// <param name="expires">過期時間</param>
public static void AddCookie(string cookieName, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
AddCookie(cookie);
}
/// <summary>
/// 添加為Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
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);
}
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="cookie"></param>
public static void AddCookie(HttpCookie cookie)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
//指定客戶端腳本是否可以訪問[默認為false]
cookie.HttpOnly = true;
//指定統一的Path,比便能通存通取
cookie.Path = "/";
//設置跨域,這樣在其它二級域名下就都可以訪問到了
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}
#endregion
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved