/// <summary>
/// CookieHelper操作工具類
/// </summary>
public class CookieHelper
{
/// <summary>
/// 設置cookie值
/// </summary>
/// <param name="cookiename">cookie名稱</param>
/// <param name="key">鍵</param>
/// <param name="val">值</param>
/// <param name="expires">失效期</param>
public static void WriteCookie(string cookiename, string key, string val, DateTime expires)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
HttpCookie cookie = Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
cookie.Domain = ConfigHelper.SiteLoginDomain;
}
if (val == null)
{
cookie.Values.Remove(key);
}
else
{
cookie.Values[key] = val;
cookie.Expires = expires;
}
Response.Cookies.Add(cookie);
}
public static void WriteCookie(string cookiename, string key, string val, int expires)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
HttpCookie cookie = Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
cookie.Domain = ConfigHelper.SiteLoginDomain;
}
if (val == null)
{
cookie.Values.Remove(key);
}
else
{
cookie.Values[key] = val;
cookie.Expires = DateTime.Now.AddMinutes(expires);
}
Response.Cookies.Add(cookie);
}
/// <summary>
/// 寫cookie值
/// </summary>
/// <param name="strName">名稱</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 寫cookie值
/// </summary>
/// <param name="strName">名稱</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string key, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie[key] = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 寫cookie值
/// </summary>
/// <param name="strName">名稱</param>
/// <param name="strValue">值</param>
/// <param name="strValue">過期時間(分鐘)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 讀cookie值
/// </summary>
/// <param name="strName">名稱</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
return "";
}
/// <summary>
/// 讀cookie值
/// </summary>
/// <param name="strName">名稱</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName, string key)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
return HttpContext.Current.Request.Cookies[strName][key].ToString();
return "";
}
/// <summary>
/// 移除指定名稱的cookie對象中的集合對
/// </summary>
/// <param name="cookieName">cookie名稱</param>
public static void RemoveCookie(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
HttpResponse Response = HttpContext.Current.Response;
if (cookie != null)
{
cookie.Values.Clear();
cookie.Domain = ConfigHelper.SiteLoginDomain;
cookie.Expires = DateTime.Now.AddDays(-10000d);
Response.Cookies.Add(cookie);
}
}
/// <summary>
/// 移除指定名稱的cookie對象中的集合中的鍵值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
public static void RemoveCookieKey(string cookieName, string key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
HttpResponse Response = HttpContext.Current.Response;
if (cookie != null)
{
cookie.Values.Remove(key);
Response.Cookies.Add(cookie);
}
}
/// <summary>
/// 移除指定名稱的cookie對象中的集合中的鍵值
/// </summary>
/// <param name="cookieName">cookie名稱</param>
/// <param name="index">鍵值名</param>
public static void RemoveCookieKey(string cookieName, int index)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
HttpResponse Response = HttpContext.Current.Response;
if (cookie != null)
{
cookie.Values.Remove(cookie.Values.GetKey(index));
Response.Cookies.Add(cookie);
}
}
}
Cookie
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乡范,“玉大人配名,你說我怎么就攤上這事〗荆” “怎么了渠脉?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長瓶佳。 經(jīng)常有香客問我芋膘,道長,這世上最難降的妖魔是什么霸饲? 我笑而不...
- 正文 為了忘掉前任为朋,我火速辦了婚禮,結(jié)果婚禮上厚脉,老公的妹妹穿的比我還像新娘习寸。我一直安慰自己,他們只是感情好傻工,可當我...
- 文/花漫 我一把揭開白布霞溪。 她就那樣靜靜地躺著孵滞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鸯匹。 梳的紋絲不亂的頭發(fā)上坊饶,一...
- 文/蒼蘭香墨 我猛地睜開眼糟秘,長吁一口氣:“原來是場噩夢啊……” “哼简逮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起尿赚,我...
- 正文 年R本政府宣布,位于F島的核電站琴庵,受9級特大地震影響误算,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜迷殿,卻給世界環(huán)境...
- 文/蒙蒙 一尉桩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贪庙,春花似錦蜘犁、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽奏窑。三九已至,卻和暖如春屈扎,著一層夾襖步出監(jiān)牢的瞬間埃唯,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 一士复、背景介紹 域名是什么骄呼? 要解釋域名是什么,我們應當知道一些互聯(lián)網(wǎng)的基本概念 我們實現(xiàn)互聯(lián)網(wǎng)的方式中有一...
- 一.背景介紹 1.什么是IP地址 互聯(lián)網(wǎng)協(xié)議地址(英語:Internet Protocol Address堂竟,又譯為...
- JS 設置cookie、讀取cookie税稼、刪除cookie 通常瀏覽器在第一次頁面加載時候會幫我們把網(wǎng)站的圖片數(shù)據(jù)...
- 這幾天烦秩,APP需要添加一組動畫垮斯,然后設計同事用spine導出了一些動畫給我,下面就是我的探索添加動畫的過程只祠。 有幾...