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

asp.net下cookies操作完美代码

程序员文章站 2024-03-08 18:52:47
复制代码 代码如下:using system; using system.web; namespace moosoft.oa.public { ///
复制代码 代码如下:

using system;
using system.web;
namespace moosoft.oa.public
{
/// <summary>
/// cookie帮助类
/// </summary>
public class cookieshelper
{
#region 获取cookie
/// <summary>
/// 获得cookie的值
/// </summary>
/// <param name="cookiename"></param>
/// <returns></returns>
public static string getcookievalue(string cookiename)
{
return getcookievalue(cookiename, null);
}
/// <summary>
/// 获得cookie的值
/// </summary>
/// <param name="cookiename"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string getcookievalue(string cookiename, string key)
{
httprequest request = httpcontext.current.request;
if (request != null)
return getcookievalue(request.cookies[cookiename], key);
return "";
}
/// <summary>
/// 获得cookie的子键值
/// </summary>
/// <param name="cookie"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string getcookievalue(httpcookie cookie, string key)
{
if (cookie != null)
{
if (!string.isnullorempty(key) && cookie.haskeys)
return cookie.values[key];
else
return cookie.value;
}
return "";
}
/// <summary>
/// 获得cookie
/// </summary>
/// <param name="cookiename"></param>
/// <returns></returns>
public static httpcookie getcookie(string cookiename)
{
httprequest request = httpcontext.current.request;
if (request != null)
return request.cookies[cookiename];
return null;
}
#endregion
#region 删除cookie
/// <summary>
/// 删除cookie
/// </summary>
/// <param name="cookiename"></param>
public static void removecookie(string cookiename)
{
removecookie(cookiename, null);
}
/// <summary>
/// 删除cookie的子键
/// </summary>
/// <param name="cookiename"></param>
/// <param name="key"></param>
public static void removecookie(string cookiename, string key)
{
httpresponse response = httpcontext.current.response;
if (response != null)
{
httpcookie cookie = response.cookies[cookiename];
if (cookie != null)
{
if (!string.isnullorempty(key) && cookie.haskeys)
cookie.values.remove(key);
else
response.cookies.remove(cookiename);
}
}
}
#endregion
#region 设置/修改cookie
/// <summary>
/// 设置cookie子键的值
/// </summary>
/// <param name="cookiename"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void setcookie(string cookiename, string key, string value)
{
setcookie(cookiename, key, value, null);
}
/// <summary>
/// 设置cookie值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void setcookie(string key, string value)
{
setcookie(key, null, value, null);
}
/// <summary>
/// 设置cookie值和过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void setcookie(string key, string value, datetime expires)
{
setcookie(key, null, value, expires);
}
/// <summary>
/// 设置cookie过期时间
/// </summary>
/// <param name="cookiename"></param>
/// <param name="expires"></param>
public static void setcookie(string cookiename, datetime expires)
{
setcookie(cookiename, null, null, expires);
}
/// <summary>
/// 设置cookie
/// </summary>
/// <param name="cookiename"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void setcookie(string cookiename, string key, string value, datetime? expires)
{
httpresponse response = httpcontext.current.response;
if (response != null)
{
httpcookie cookie = response.cookies[cookiename];
if (cookie != null)
{
if (!string.isnullorempty(key) && cookie.haskeys)
cookie.values.set(key, value);
else
if (!string.isnullorempty(value))
cookie.value = value;
if (expires != null)
cookie.expires = expires.value;
response.setcookie(cookie);
}
}
}
#endregion
#region 添加cookie
/// <summary>
/// 添加cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void addcookie(string key, string value)
{
addcookie(new httpcookie(key, value));
}
/// <summary>
/// 添加cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void addcookie(string key, string value, datetime expires)
{
httpcookie cookie = new httpcookie(key, value);
cookie.expires = expires;
addcookie(cookie);
}
/// <summary>
/// 添加为cookie.values集合
/// </summary>
/// <param name="cookiename"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void addcookie(string cookiename, string key, string value)
{
httpcookie cookie = new httpcookie(cookiename);
cookie.values.add(key, value);
addcookie(cookie);
}
/// <summary>
/// 添加为cookie集合
/// </summary>
/// <param name="cookiename">cookie名称</param>
/// <param name="expires">过期时间</param>
public static void addcookie(string cookiename, datetime expires)
{
httpcookie cookie = new httpcookie(cookiename);
cookie.expires = expires;
addcookie(cookie);
}
/// <summary>
/// 添加为cookie.values集合
/// </summary>
/// <param name="cookiename"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void addcookie(string cookiename, string key, string value, datetime expires)
{
httpcookie cookie = new httpcookie(cookiename);
cookie.expires = expires;
cookie.values.add(key, value);
addcookie(cookie);
}
/// <summary>
/// 添加cookie
/// </summary>
/// <param name="cookie"></param>
public static void addcookie(httpcookie cookie)
{
httpresponse response = httpcontext.current.response;
if (response != null)
{
//指定客户端脚本是否可以访问[默认为false]
cookie.httponly = true;
//指定统一的path,比便能通存通取
cookie.path = "/";
//设置跨域,这样在其它二级域名下就都可以访问到了
//cookie.domain = "chinesecoo.com";
response.appendcookie(cookie);
}
}
#endregion
}
}