国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net下cookies操作完美代碼

2024-07-10 13:26:22
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:


using System;
using System.Web;
namespace Moosoft.OA.Public
{
/// <summary>
/// Cookie幫助類
/// </summary>
public class CookiesHelper
{
#region 獲取Cookie
/// <summary>
/// 獲得Cookie的值
/// </summary>
/// <param></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName)
{
return GetCookieValue(cookieName, null);
}
/// <summary>
/// 獲得Cookie的值
/// </summary>
/// <param></param>
/// <param></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></param>
/// <param></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></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></param>
public static void RemoveCookie(string cookieName)
{
RemoveCookie(cookieName, null);
}
/// <summary>
/// 刪除Cookie的子鍵
/// </summary>
/// <param></param>
/// <param></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></param>
/// <param></param>
/// <param></param>
public static void SetCookie(string cookieName, string key, string value)
{
SetCookie(cookieName, key, value, null);
}
/// <summary>
/// 設(shè)置Cookie值
/// </summary>
/// <param></param>
/// <param></param>
public static void SetCookie(string key, string value)
{
SetCookie(key, null, value, null);
}
/// <summary>
/// 設(shè)置Cookie值和過期時(shí)間
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
public static void SetCookie(string key, string value, DateTime expires)
{
SetCookie(key, null, value, expires);
}
/// <summary>
/// 設(shè)置Cookie過期時(shí)間
/// </summary>
/// <param></param>
/// <param></param>
public static void SetCookie(string cookieName, DateTime expires)
{
SetCookie(cookieName, null, null, expires);
}
/// <summary>
/// 設(shè)置Cookie
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></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></param>
/// <param></param>
public static void AddCookie(string key, string value)
{
AddCookie(new HttpCookie(key, value));
}
/// <summary>
/// 添加Cookie
/// </summary>
/// <param></param>
/// <param></param>
/// <param></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></param>
/// <param></param>
/// <param></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>Cookie名稱</param>
/// <param>過期時(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></param>
/// <param></param>
/// <param></param>
/// <param></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></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 = "http://m.survivalescaperooms.com/";
//設(shè)置跨域,這樣在其它二級(jí)域名下就都可以訪問到了
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}
#endregion
}
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜阳市| 江都市| 宜宾县| 秭归县| 湄潭县| 石景山区| 余江县| 克拉玛依市| 辉南县| 漯河市| 南部县| 仁寿县| 招远市| 孝感市| 会泽县| 巴中市| 浮梁县| 安平县| 维西| 盐津县| 靖江市| 荥阳市| 阜阳市| 西和县| 霞浦县| 荣昌县| 本溪市| 龙南县| 溧水县| 田东县| 河源市| 正蓝旗| 边坝县| 交城县| 星座| 满城县| 盐源县| 祁阳县| 芒康县| 竹北市| 射洪县|