国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 嘉兴市| 湛江市| 定南县| 泾川县| 兴海县| 环江| 清徐县| 河曲县| 敦煌市| 京山县| 壤塘县| 崇州市| 拜泉县| 武宣县| 娱乐| 米易县| 三门峡市| 中阳县| 杭州市| 平和县| 宝应县| 莲花县| 湖口县| 锦州市| 大同市| 伊宁市| 礼泉县| 南岸区| 墨竹工卡县| 荔浦县| 清丰县| 吐鲁番市| 河津市| 长葛市| 邵东县| 江北区| 横峰县| 方山县| 雷州市| 二连浩特市| 阿克陶县|