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值和過期時(shí)間
/// </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過期時(shí)間
/// </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">過期時(shí)間</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)
{
//指定客戶端腳本是否可以訪問[默認(rèn)為false]
cookie.HttpOnly = true;
//指定統(tǒng)一的Path,比便能通存通取
cookie.Path = "/";
//設(shè)置跨域,這樣在其它二級(jí)域名下就都可以訪問到了
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}
#endregion
}
}
- asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法
- asp.net各種cookie代碼和解析實(shí)例
- asp.net 操作cookie的簡(jiǎn)單實(shí)例
- Asp.net cookie的處理流程深入分析
- asp.net關(guān)于Cookie跨域(域名)的問題
- asp.net中的cookie使用介紹
- Asp.net 基于Cookie簡(jiǎn)易的權(quán)限判斷
- asp.net Cookie操作類
- ASP.NET Cookie 操作實(shí)現(xiàn)
- asp.net cookie的讀寫實(shí)例
- asp.net cookie清除的代碼
- ASP.NET登出系統(tǒng)并清除Cookie
相關(guān)文章
ASP.NET Core實(shí)現(xiàn)多文件上傳
這篇文章介紹了ASP.NET Core實(shí)現(xiàn)多文件上傳的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01ASP.NET使用Subtract方法獲取兩個(gè)日期之間的天數(shù)
本節(jié)主要介紹了ASP.NET使用Subtract方法獲取兩個(gè)日期之間的天數(shù),需要的朋友可以參考下2014-08-08asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的數(shù)據(jù)表格的方法
導(dǎo)出word 文檔,要求這個(gè)文檔的格式不是固定的,用戶可以隨便的調(diào)整,導(dǎo)出內(nèi)容中的數(shù)據(jù)表格列是動(dòng)態(tài)的,例如要求導(dǎo)出姓名和性別,你就要導(dǎo)出這兩列的數(shù)據(jù),而且這個(gè)文檔不是導(dǎo)出來之后再調(diào)整而是導(dǎo)出來后已經(jīng)是調(diào)整過了的。2010-04-04.Net 下區(qū)別使用 ByRef/ByVal 的重要性 分享
這篇文章介紹了.Net 下區(qū)別使用 ByRef/ByVal 的重要性,有需要的朋友可以參考一下2013-07-07asp.net實(shí)現(xiàn)存儲(chǔ)和讀取數(shù)據(jù)庫圖片
這篇文章主要為大家詳細(xì)介紹了asp.net實(shí)現(xiàn)存儲(chǔ)和讀取數(shù)據(jù)庫圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11ASP.Net?Core讀取配置文件的三種方法小結(jié)
ASP.NET?Core支持多種格式的配置文件,如JSON、XML、INI等,本文就來介紹一下ASP.Net?Core讀取配置文件的三種方法,感興趣的可以了解一下2024-02-02document.getElementsByName和document.getElementById 在IE與FF中不同
今天在<asp:radiobuttonlist/>中使用教本的的時(shí)候才注意到原來 document.getElementsByName 、document.getElementById 在IE與FF中有著不同實(shí)現(xiàn)。2008-12-12.net 通過URL推送POST數(shù)據(jù)具體實(shí)現(xiàn)
這篇文章主要介紹了.net 通過URL推送POST數(shù)據(jù)具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12