Copy Code code as follows:
public class Bscookie
{
Cookie for Action
Private HttpCookie _thecookie;
The name of the corresponding cookie
private string _cookiename;
private bool _httponly = true;
<summary>
Allow access only on server side, default only on service side
</summary>
public bool HttpOnly
{
get {return _httponly;}
set {_httponly = value;}
}
Private double _expireminutes;
<summary>
The effective storage time of cookies, expressed in minutes
</summary>
Public double Expireminutes
{
get {return _expireminutes;}
set {_expireminutes = value;}
}
Public Bscookie (String name,double expireminutes)
{
_cookiename = name;
_expireminutes = expireminutes;
}
<summary>
Read the corresponding cookie
</summary>
<param name= "Name" ></param>
<returns></returns>
Private HttpCookie Getcookiereq ()
{
HttpRequest request = HttpContext.Current.Request;
if (Request!= null)
{
HttpCookie cookies = Request. Cookies[_cookiename];
if (cookie!= null)
{
return cookie;
}
}
return null;
}
<summary>
Set the corresponding 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>
Set only the primary key
</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>
Set a set of primary keys
</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>
Get a single cookie primary key value
</summary>
<param name= "Name" ></param>
<param name= "Exminutes" > Default time for cookies that need to be extended </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>
Gets a set of corresponding cookie values
</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>
Get a single value inside a group
</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;
}
}
Demonstrates the basic operation of cookies in asp.net.
Copy Code code as follows:
Imports System.Web.HttpContext
Public Class Cookieframework
' Write a single 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
' Add subkeys to the cookie collection
Public Shared Function writecookies (ByVal cookiename As String, ByVal Cookieitem as String, ByVal Itemvalue as String, Byv Al 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
' Read a single 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
' Reads the contents of the child items in the cookie collection
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
' Delete the entire 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
' Delete subkeys in the cookie collection
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