关闭

asp.net cookie操作-添加cookie,添加键值,移除cookie,移除键值

标签: asp.netstringnull存储服务器cookies
3424人阅读 评论(0) 收藏 举报
分类:
public class Cookies
{
	/// <summary>
	/// 添加cookie
	/// </summary>
	/// <param name="cookieName"></param>
	/// <param name="value"></param>
	public static void Add(string cookieName, string value)
	{
		HttpCookie cookie = new HttpCookie(HttpUtility.UrlEncode(cookieName), HttpUtility.UrlEncode(value));
		HttpContext.Current.Response.AppendCookie(cookie);
	}

	/// <summary>
	/// 给指定的cookie的指定键添加值
	/// </summary>
	/// <param name="cookieName">cookie的名称</param>
	/// <param name="keyName">要添加的键</param>
	/// <param name="value">要添加的值</param>
	public static void Add(string cookieName, string keyName, string value)
	{
		HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
		//如果cookie=null则新建一个cookie
		if (cookie == null)
			cookie = new HttpCookie(HttpUtility.UrlEncode(cookieName));
		//如果该键不存在,则新建一个键值并赋值;否则直接赋值
		if (string.IsNullOrEmpty(cookie.Values[keyName]))
			cookie.Values.Add(keyName, HttpUtility.UrlEncode(value));
		else
			cookie.Values[keyName] = HttpUtility.UrlEncode(value);

		HttpContext.Current.Response.AppendCookie(cookie);
	}

	/// <summary>
	/// 读取Cookie
	/// </summary>
	/// <param name="key"></param>
	/// <returns></returns>
	public static string GetValue(string cookieName)
	{
		string value = HttpContext.Current.Request.Cookies[HttpUtility.UrlEncode(cookieName)].Value;
		return string.IsNullOrEmpty(value) ? string.Empty : HttpUtility.UrlDecode(value);
	}

	/// <summary>
	/// 读取指定Cookie里的指定键的值
	/// </summary>
	/// <param name="cookieName">cookie名称</param>
	/// <param name="keyName">cookie的键</param>
	/// <returns></returns>
	public static string GetValue(string cookieName, string keyName)
	{
		HttpCookie cookie = HttpContext.Current.Request.Cookies[HttpUtility.UrlEncode(cookieName)];
		if (cookie == null)
			return string.Empty;
		string value = cookie.Values[HttpUtility.UrlEncode(keyName)];
		return string.IsNullOrEmpty(value) ? string.Empty : HttpUtility.UrlDecode(value);
	}

	/// <summary>
	/// 获取指定cookie中所有的键
	/// </summary>
	/// <param name="cookieName"></param>
	/// <returns></returns>
	public static string[] GetValues(string cookieName)
	{
		if (HttpContext.Current.Request.Cookies[cookieName] == null)
			return new string[0];
		string[] keys = HttpContext.Current.Request.Cookies[cookieName].Values.AllKeys;
		for (int i = 0; i < keys.Length; i++)
		{
			keys[i] = HttpUtility.UrlDecode(keys[i]);
		}
		return keys;
	}

	/// <summary>
	/// 移除指定的Cookie
	/// </summary>
	/// <param name="cookieName"></param>
	public static void Remove(string cookieName)
	{
		HttpContext.Current.Response.Cookies.Remove(HttpUtility.UrlEncode(cookieName));
		HttpContext.Current.Response.Cookies[HttpUtility.UrlEncode(cookieName)].Expires = DateTime.Now.AddDays(-1);
	}
	/// <summary>
	/// 移除cookie中指定的键,若是最后一个键则移除这个cookie
	/// </summary>
	/// <param name="cookieName"></param>
	/// <param name="keyName"></param>
	public static void Remove(string cookieName, string keyName)
	{
		HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
		if (cookie != null)
		{
			if (cookie.Values.Count > 0)
			{
				if (cookie.Values.Count == 1)
				{
					//若是最后一个键则移除这个cookie,否则会多出一个空值
					cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
					cookie.Expires = DateTime.Now.AddDays(-1);
				}
				else
				{
					cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
				}
				HttpContext.Current.Response.Cookies.Add(cookie);
			}
		}
	}
}

首先要了解Cookie是存在客户端的。

添加cookie是向客户端添加cookie,所以要用(Response)HttpContext.Current.Response.AppendCookie(cookie);

获取cookie是获取客户端提交上来的cookie,所以要用(Request)HttpContext.Current.Request.Cookies[cookieName];

服务器端没有权限删除客户端上的文件,所以服务器端,不能删除cookie。但可以让浏览器删除,即让cookie过期。

关于cookie读取中文cookie时出现乱码:存储中文时要进行编码,读取中文时则进行解码,这样可以避免出现乱码。

cookie中存储数据的格式是以值键对的形式存储的:即 key1=value1&key2=value2;

所以

cookie.Values.Add(key1, value1);
和
cookie.Value=cookie.Value+"&key1=value1"
是等同的。但是后者在Request.Cookies[cookieName].Values.Count时则会变成1。
可能是加上新值之后编码不一所以count的时候变成了1


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:431949次
    • 积分:3019
    • 等级:
    • 排名:第14386名
    • 原创:38篇
    • 转载:3篇
    • 译文:1篇
    • 评论:66条
    最新评论