asp.net下cookies操作完美代碼
更新時間:2010年03月16日 19:34:40 作者:
asp.net下cookies操作完美代碼,需要的朋友可以參考下。
復制代碼 代碼如下:
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 設(shè)置/修改Cookie
/// <summary>
/// 設(shè)置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>
/// 設(shè)置Cookie值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string key, string value)
{
SetCookie(key, null, value, null);
}
/// <summary>
/// 設(shè)置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>
/// 設(shè)置Cookie過期時間
/// </summary>
/// <param name="cookieName"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName, DateTime expires)
{
SetCookie(cookieName, null, null, expires);
}
/// <summary>
/// 設(shè)置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;
//指定統(tǒng)一的Path,比便能通存通取
cookie.Path = "/";
//設(shè)置跨域,這樣在其它二級域名下就都可以訪問到了
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}
#endregion
}
}
相關(guān)文章
ASP.NET使用Subtract方法獲取兩個日期之間的天數(shù)
本節(jié)主要介紹了ASP.NET使用Subtract方法獲取兩個日期之間的天數(shù),需要的朋友可以參考下2014-08-08
asp.net下用Aspose.Words for .NET動態(tài)生成word文檔中的數(shù)據(jù)表格的方法
導出word 文檔,要求這個文檔的格式不是固定的,用戶可以隨便的調(diào)整,導出內(nèi)容中的數(shù)據(jù)表格列是動態(tài)的,例如要求導出姓名和性別,你就要導出這兩列的數(shù)據(jù),而且這個文檔不是導出來之后再調(diào)整而是導出來后已經(jīng)是調(diào)整過了的。2010-04-04
.Net 下區(qū)別使用 ByRef/ByVal 的重要性 分享
這篇文章介紹了.Net 下區(qū)別使用 ByRef/ByVal 的重要性,有需要的朋友可以參考一下2013-07-07
asp.net實現(xiàn)存儲和讀取數(shù)據(jù)庫圖片
這篇文章主要為大家詳細介紹了asp.net實現(xiàn)存儲和讀取數(shù)據(jù)庫圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
ASP.Net?Core讀取配置文件的三種方法小結(jié)
ASP.NET?Core支持多種格式的配置文件,如JSON、XML、INI等,本文就來介紹一下ASP.Net?Core讀取配置文件的三種方法,感興趣的可以了解一下2024-02-02
document.getElementsByName和document.getElementById 在IE與FF中不同
今天在<asp:radiobuttonlist/>中使用教本的的時候才注意到原來 document.getElementsByName 、document.getElementById 在IE與FF中有著不同實現(xiàn)。2008-12-12
.net 通過URL推送POST數(shù)據(jù)具體實現(xiàn)
這篇文章主要介紹了.net 通過URL推送POST數(shù)據(jù)具體實現(xiàn),有需要的朋友可以參考一下2013-12-12

