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

首頁(yè) > 編程 > .NET > 正文

asp.net Cookie操作類(lèi)

2024-07-10 13:18:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(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操作類(lèi)
/// </summary>
public class Cookie
{
/// <summary>
/// 保存一個(gè)Cookie
/// </summary>
/// <param>Cookie名稱(chēng)</param>
/// <param>Cookie值</param>
/// <param>Cookie過(guò)期時(shí)間(小時(shí)),0為關(guān)閉頁(yè)面失效</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>Cookie名稱(chēng)</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>Cookie名稱(chēng)</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);
}
}
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 陕西省| 藁城市| 进贤县| 曲沃县| 临夏县| 双城市| 甘德县| 苏尼特右旗| 丰县| 鄢陵县| 榆树市| 六盘水市| 彭阳县| 宾川县| 兴安盟| 井研县| 宁晋县| 七台河市| 郯城县| 布拖县| 平昌县| 安康市| 苍南县| 肇东市| 阿尔山市| 鲁山县| 四会市| 芜湖县| 满洲里市| 和硕县| 玛曲县| 衡阳市| 郓城县| 遂昌县| 万山特区| 广德县| 舟山市| 高唐县| 南陵县| 抚顺市| 鹿邑县|