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

首頁 > 編程 > .NET > 正文

asp.net之Cookie的操作類

2024-07-10 12:53:35
字體:
供稿:網(wǎng)友

在客戶端存儲(chǔ)cookie是ASP的一種方法,而.NET會(huì)話狀態(tài)將請(qǐng)求與會(huì)話關(guān)聯(lián),其實(shí)cookie也可以直接用于請(qǐng)求保存數(shù)據(jù),但是數(shù)據(jù)隨后存儲(chǔ)在客戶端,并隨每個(gè)請(qǐng)求一起發(fā)送到服務(wù)器,下面就跟著錯(cuò)新技術(shù)頻道小編的步伐來學(xué)習(xí)asp.net之Cookie的操作類。

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace Jhgl.Smart
{
/// <summary>
/// Cookie操作類
/// </summary>
public class Cookie
{
/// <summary>
/// 保存一個(gè)Cookie
/// </summary>
/// <param name="CookieName">Cookie名稱</param>
/// <param name="CookieValue">Cookie值</param>
/// <param name="CookieTime">Cookie過期時(shí)間(小時(shí)),0為關(guān)閉頁面失效</param>
public static void SaveCookie(string CookieName, string CookieValue, double CookieTime)
{
HttpCookie myCookie = new HttpCookie(CookieName);
DateTime now = DateTime.Now;
myCookie.Value = CookieValue;

if (CookieTime != 0)
{
//有兩種方法,第一方法設(shè)置Cookie時(shí)間的話,關(guān)閉瀏覽器不會(huì)自動(dòng)清除Cookie
//第二方法不設(shè)置Cookie時(shí)間的話,關(guān)閉瀏覽器會(huì)自動(dòng)清除Cookie ,但是有效期
//多久還未得到證實(shí)。
myCookie.Expires = now.AddDays(CookieTime);
if (HttpContext.Current.Response.Cookies[CookieName] != null)
HttpContext.Current.Response.Cookies.Remove(CookieName);

HttpContext.Current.Response.Cookies.Add(myCookie);
}
else
{
if (HttpContext.Current.Response.Cookies[CookieName] != null)
HttpContext.Current.Response.Cookies.Remove(CookieName);

HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
/// <summary>
/// 取得CookieValue
/// </summary>
/// <param name="CookieName">Cookie名稱</param>
/// <returns>Cookie的值</returns>
public static string GetCookie(string CookieName)
{
HttpCookie myCookie = new HttpCookie(CookieName);
myCookie = HttpContext.Current.Request.Cookies[CookieName];

if (myCookie != null)
return myCookie.Value;
else
return null;
}
/// <summary>
/// 清除CookieValue
/// </summary>
/// <param name="CookieName">Cookie名稱</param>
public static void ClearCookie(string CookieName)
{
HttpCookie myCookie = new HttpCookie(CookieName);
DateTime now = DateTime.Now;

myCookie.Expires = now.AddYears(-2);

HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
}

?以上就是錯(cuò)新技術(shù)頻道小編為各位朋友們介紹的asp.net之Cookie的操作類,各位朋友們看完之后,是不是非常的清楚了呢?希望上面的內(nèi)容可以幫助到大家!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀集县| 蓝田县| 昌都县| 凌海市| 莱州市| 昌黎县| 越西县| 茂名市| 台湾省| 宾阳县| 正定县| 西藏| 灌云县| 琼结县| 长寿区| 会同县| 荃湾区| 独山县| 抚州市| 通河县| 南京市| 独山县| 沈阳市| 施甸县| 玉树县| 兴国县| 新和县| 大安市| 类乌齐县| 天全县| 福海县| 江安县| 石泉县| 永福县| 全椒县| 衡水市| 新兴县| 定兴县| 淮阳县| 襄城县| 遵义市|