欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET下对cookies的操作实现代码

程序员文章站 2024-03-07 19:09:09
复制代码 代码如下: public class bscookie { //操作的cookie private httpcookie _thecookie; //对应的coo...
复制代码 代码如下:

public class bscookie
{
//操作的cookie
private httpcookie _thecookie;
//对应的cookie的名称
private string _cookiename;
private bool _httponly = true;
/// <summary>
/// 是否只允许在服务器端访问,默认只允许在服务端访问
/// </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>
/// 读取对应的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>
/// 设置对应的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>
/// 仅设置主键的
/// </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>
/// 设置一组主键
/// </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的默认时间</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>
/// 获取一组对应的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的基本操作。
复制代码 代码如下:

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集合添加子项
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集合中的子项内容
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集合中的子项
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