最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET下對cookies的操作實(shí)現(xiàn)代碼

 更新時間:2011年10月11日 14:58:40   作者:  
ASP.NET下對cookies的操作實(shí)現(xiàn)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:

public class BsCookie
{
//操作的cookie
private HttpCookie _theCookie;
//對應(yīng)的cookie的名稱
private string _cookieName;
private bool _httpOnly = true;
/// <summary>
/// 是否只允許在服務(wù)器端訪問,默認(rèn)只允許在服務(wù)端訪問
/// </summary>
public bool HttpOnly
{
get { return _httpOnly; }
set { _httpOnly = value; }
}
private double _expireMinutes;
/// <summary>
/// Cookies有效的存放時間,以分鐘表示
/// </summary>
public double ExpireMinutes
{
get { return _expireMinutes; }
set { _expireMinutes = value; }
}
public BsCookie(string name,double expireMinutes)
{
_cookieName = name;
_expireMinutes = expireMinutes;
}
/// <summary>
/// 讀取對應(yīng)的cookie
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private HttpCookie GetCookieReq()
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
{
HttpCookie cookie = request.Cookies[_cookieName];
if (cookie != null)
{
return cookie;
}
}
return null;
}
/// <summary>
/// 設(shè)置對應(yīng)的cookie
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private HttpCookie GetCookieResponse()
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[_cookieName];
if (cookie != null)
{
return cookie;
}
}
return new HttpCookie(_cookieName);
}
/// <summary>
/// 僅設(shè)置主鍵的
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="exMinutes"></param>
public void SetCookie(string value)
{
_theCookie = GetCookieResponse();
_theCookie.Value = HttpUtility.HtmlEncode(AllCommon.Encrypt(value));
if (Math.Abs(_expireMinutes) > 1)
{
_theCookie.Expires = DateTime.Now.AddMinutes(_expireMinutes);
}
_theCookie.HttpOnly = _httpOnly;
}
/// <summary>
/// 設(shè)置一組主鍵
/// </summary>
/// <param name="name"></param>
/// <param name="keys"></param>
/// <param name="exMinutes"></param>
public void SetCookie(Hashtable keys)
{
_theCookie = GetCookieResponse();
foreach (DictionaryEntry de in keys)
{
_theCookie.Values[de.Key.ToString()] = HttpUtility.HtmlEncode(AllCommon.Encrypt(de.Value.ToString()));
}
if (Math.Abs(_expireMinutes) > 1)
{
_theCookie.Expires = DateTime.Now.AddMinutes(_expireMinutes);
}
_theCookie.HttpOnly = _httpOnly;
}
/// <summary>
/// 獲取單一的cookie主鍵值
/// </summary>
/// <param name="name"></param>
/// <param name="exMinutes">需要延長的cookie的默認(rèn)時間</param>
/// <returns></returns>
public string GetCookie()
{
_theCookie = GetCookieReq();
if (_theCookie == null)
{
return string.Empty;
}
string thevalue = AllCommon.Decrypt(HttpUtility.HtmlDecode(_theCookie.Value));
if (thevalue.Length > 0)
{
HttpCookie serverCookie = GetCookieResponse();
if (Math.Abs(_expireMinutes) > 1)
{
serverCookie.Expires = DateTime.Now.AddMinutes(_expireMinutes);
}
}
return thevalue;
}
/// <summary>
/// 獲取一組對應(yīng)的cookie值
/// </summary>
/// <param name="name"></param>
/// <param name="exMinutes"></param>
/// <returns></returns>
public Hashtable GetCookiesKeys()
{
_theCookie = GetCookieReq();
if (_theCookie == null)
{
return null;
}
string[] keys = _theCookie.Values.AllKeys;
if (keys.Length > 0)
{
Hashtable keyHash = new Hashtable();
foreach (string key in keys)
{
keyHash.Add(key, AllCommon.Decrypt(HttpUtility.HtmlDecode(_theCookie.Values[key])));
}
HttpCookie serverCookie = GetCookieResponse();
if (Math.Abs(_expireMinutes) > 1)
{
serverCookie.Expires = DateTime.Now.AddMinutes(_expireMinutes);
}
return keyHash;
}
return null;
}
/// <summary>
/// 獲取一組里面的單一個值
/// </summary>
/// <param name="keyname"></param>
/// <param name="exMinutes"></param>
/// <returns></returns>
public string GetCookieKV(string keyname)
{
_theCookie = GetCookieReq();
if (_theCookie == null)
{
return string.Empty;
}
string result=AllCommon.Decrypt(HttpUtility.HtmlDecode(_theCookie.Values[keyname]));
if (result.Length > 0)
{
HttpCookie serverCookie = GetCookieResponse();
if (Math.Abs(_expireMinutes) > 1 && serverCookie != null)
{
serverCookie.Expires = DateTime.Now.AddMinutes(_expireMinutes);
}
}
return result;
}
}

演示Asp.Net中對Cookie的基本操作。
復(fù)制代碼 代碼如下:

Imports System.Web.HttpContext
Public Class CookieFramework
'寫入單個Cookie
Public Shared Function WriteCookie(ByVal CookieName As String, ByVal CookieValue As String, ByVal ExpiresDate As Integer) As Boolean
Dim aCookie As New HttpCookie(CookieName)
aCookie.Value = CookieValue
aCookie.Expires = DateTime.Now.AddDays(ExpiresDate)
System.Web.HttpContext.Current.Response.Cookies.Add(aCookie)
End Function

'給Cookie集合添加子項(xiàng)
Public Shared Function WriteCookies(ByVal CookieName As String, ByVal CookieItem As String, ByVal ItemValue As String, ByVal ExpiresDate As Integer) As Boolean
Dim aCookie As HttpCookie
If Current.Request.Cookies(CookieName) Is Nothing Then
aCookie = New HttpCookie(CookieName)
Else
aCookie = Current.Request.Cookies(CookieName)
End If
aCookie.Values(CookieItem) = ItemValue
aCookie.Expires = DateTime.Now.AddDays(ExpiresDate)
System.Web.HttpContext.Current.Response.Cookies.Add(aCookie)
End Function
'讀取單個Cookie
Public Shared Function ReadCookie(ByVal CookieName As String) As String
If Current.Request.Cookies(CookieName) Is Nothing Then
Return Nothing
Else
Return Current.Request.Cookies(CookieName).Value
End If
End Function

'讀取Cookie集合中的子項(xiàng)內(nèi)容
Public Shared Function ReadCookies(ByVal CookieName As String, ByVal CookieItem As String) As String
If Current.Request.Cookies(CookieName) Is Nothing Then
Return Nothing
Else
If Current.Request.Cookies(CookieName).Values(CookieItem) Is Nothing Then
Return Nothing
Else
Return Current.Request.Cookies(CookieName).Values(CookieItem)
End If
End If
End Function
'刪除整個Cookie
Public Shared Function DeleteCookie(ByVal CookieName As String) As Boolean
Dim aCookie As New HttpCookie(CookieName)
Dim i As Integer
Dim limit As Integer = Current.Request.Cookies.Count - 1
For i = 0 To limit
aCookie = Current.Request.Cookies(i)
aCookie.Expires = DateTime.Now.AddDays(-1)
Current.Response.Cookies.Add(aCookie)
Next
End Function
'刪除Cookie集合中的子項(xiàng)
Public Shared Function DeleteCookies(ByVal CookieName As String, ByVal ItemName As String) As Boolean
Dim aCookie As HttpCookie = Current.Request.Cookies(CookieName)
aCookie.Values.Remove(ItemName)
aCookie.Expires = DateTime.Now.AddDays(1)
Current.Response.Cookies.Add(aCookie)
End Function
End Class

相關(guān)文章

  • ASP.NET MVC自定義異常過濾器

    ASP.NET MVC自定義異常過濾器

    這篇文章介紹了ASP.NET MVC自定義異常過濾器的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能示例【附demo源碼】

    ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能示例【附demo源碼】

    這篇文章主要介紹了ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net生成驗(yàn)證碼的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2017-07-07
  • ASP.NET中讀取XML文件信息的4種方法與示例代碼

    ASP.NET中讀取XML文件信息的4種方法與示例代碼

    ASP.NET中讀取XML文件信息的4種方法與示例代碼...
    2007-03-03
  • ASP.NET防止SQL注入的方法示例

    ASP.NET防止SQL注入的方法示例

    這篇文章主要介紹了ASP.NET防止SQL注入的方法,結(jié)合具體實(shí)例形式分析了asp.net基于字符串過濾實(shí)現(xiàn)防止SQL注入的相關(guān)操作技巧,需要的朋友可以參考下
    2017-03-03
  • LiteralControl ASP.NET中的另類控件

    LiteralControl ASP.NET中的另類控件

    對于LiteralControl控件的應(yīng)用比較少,今天突然看到了,就弄個明白為好,所以總結(jié)出來,供大家一起學(xué)習(xí)
    2012-01-01
  • Hangfire在ASP.NET CORE中的簡單實(shí)現(xiàn)方法

    Hangfire在ASP.NET CORE中的簡單實(shí)現(xiàn)方法

    下面小編就為大家分享一篇Hangfire在ASP.NET CORE中的簡單實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 在 .NET 中 使用 ANTLR4構(gòu)建語法分析器的方法

    在 .NET 中 使用 ANTLR4構(gòu)建語法分析器的方法

    本文將介紹如何在 .NET 中使用 ANTLR4 構(gòu)建語法分析器,本文不會深入講解 ANTLR4 的語法規(guī)則,相關(guān)內(nèi)容可參考 ANTLR4 的官方文檔或其他資料,本文將涵蓋以下內(nèi)容:ANTLR4 的開發(fā)環(huán)境搭建、語法規(guī)則編寫、語法分析器生成以及語法分析器的使用,感興趣的朋友一起看看吧
    2025-04-04
  • ASP.NET Session對象保持會話使用說明

    ASP.NET Session對象保持會話使用說明

    ASP.NET提供了Session對象,從而允許程序員識別、存儲和處理同一個瀏覽器對象對服務(wù)器上某個特定網(wǎng)絡(luò)應(yīng)用程序的若干次請求的上下文信息
    2012-12-12
  • ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能

    ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能

    這篇文章主要介紹了ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • NetCore實(shí)現(xiàn)全局模型綁定異常信息統(tǒng)一處理(場景分析)

    NetCore實(shí)現(xiàn)全局模型綁定異常信息統(tǒng)一處理(場景分析)

    本文主要講解NetCore如何使用中間件捕獲模型綁定的異常信息,對NetCore實(shí)現(xiàn)全局模型綁定異常信息統(tǒng)一處理場景分析及實(shí)現(xiàn)代碼感興趣的朋友一起看看吧
    2021-12-12

最新評論

仙游县| 九台市| 宣威市| 云和县| 洞头县| 额尔古纳市| 宁德市| 灌南县| 九台市| 朝阳县| 德清县| 房山区| 布拖县| 日照市| 沿河| 彭阳县| 清远市| 遂昌县| 曲麻莱县| 简阳市| 甘泉县| 建阳市| 乌鲁木齐县| 清水河县| 遵化市| 潮州市| 镇沅| 雷州市| 九江市| 德钦县| 鄂伦春自治旗| 梅河口市| 通州区| 进贤县| 巫山县| 嘉荫县| 和平县| 子长县| 昌平区| 江门市| 夏河县|