asp.net Cookie操作類
更新時(shí)間:2009年12月15日 12:46:13 作者:
Cookie操作類,本人得還很不錯(cuò)哦。
復(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);
}
}
}
您可能感興趣的文章:
- asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法
- asp.net各種cookie代碼和解析實(shí)例
- asp.net 操作cookie的簡(jiǎn)單實(shí)例
- Asp.net cookie的處理流程深入分析
- asp.net關(guān)于Cookie跨域(域名)的問題
- asp.net中的cookie使用介紹
- asp.net下cookies操作完美代碼
- Asp.net 基于Cookie簡(jiǎn)易的權(quán)限判斷
- ASP.NET Cookie 操作實(shí)現(xiàn)
- asp.net cookie的讀寫實(shí)例
- asp.net cookie清除的代碼
- ASP.NET登出系統(tǒng)并清除Cookie
相關(guān)文章
一步步打造簡(jiǎn)單的MVC電商網(wǎng)站BooksStore(3)
這篇文章主要和大家一起一步步打造一個(gè)簡(jiǎn)單的MVC電商網(wǎng)站,MVC電商網(wǎng)站BooksStore第三篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04ASP.NET下使用WScript.Shell執(zhí)行命令
ASP.NET下有自己的執(zhí)行CMD命令的方式,這里用WScript.Shell似有畫蛇添足之嫌,但是我們也不能排除真的有機(jī)器禁用了.NET的相關(guān)類,未雨綢繆嘛。當(dāng)然也不僅僅局限于WScript.Shell,只要是ASP中能用的組件,統(tǒng)統(tǒng)都可以用于ASP.NET中,而且還更方便!2008-05-05ASP.NET Core實(shí)現(xiàn)多文件上傳
這篇文章介紹了ASP.NET Core實(shí)現(xiàn)多文件上傳的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01ASP.NET Core中實(shí)現(xiàn)全局異常攔截的完整步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何實(shí)現(xiàn)全局異常攔截的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01ASP.NET?Core?MVC控制器請(qǐng)求依賴注入
這篇文章介紹了ASP.NET?Core?MVC控制器請(qǐng)求依賴注入的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請(qǐng)求
這篇文章主要介紹了在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請(qǐng)求的方法,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下2021-03-03.net/c# memcached緩存獲取所有緩存鍵的方法步驟
這篇文章主要介紹了.net/c# memcached緩存獲取所有緩存鍵的方法步驟,大家參考使用吧2013-12-12