1 public class Cookies 2 { 3 /// <summary> 4 /// 添加cookie 5 /// </summary> 6 /// <param name="cookieName"></param> 7 /// <param name="value"></param> 8 public static void Add(string cookieName, string value) 9 { 10 HttpCookie cookie = new HttpCookie(HttpUtility.UrlEncode(cookieName), HttpUtility.UrlEncode(value)); 11 HttpContext.Current.Response.AppendCookie(cookie); 12 } 13 14 /// <summary> 15 /// 给指定的cookie的指定键添加值 16 /// </summary> 17 /// <param name="cookieName">cookie的名称</param> 18 /// <param name="keyName">要添加的键</param> 19 /// <param name="value">要添加的值</param> 20 public static void Add(string cookieName, string keyName, string value) 21 { 22 HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; 23 //如果cookie=null则新建一个cookie 24 if (cookie == null) 25 cookie = new HttpCookie(HttpUtility.UrlEncode(cookieName)); 26 //如果该键不存在,则新建一个键值并赋值;否则直接赋值 27 if (string.IsNullOrEmpty(cookie.Values[keyName])) 28 cookie.Values.Add(keyName, HttpUtility.UrlEncode(value)); 29 else 30 cookie.Values[keyName] = HttpUtility.UrlEncode(value); 31 32 HttpContext.Current.Response.AppendCookie(cookie); 33 } 34 35 /// <summary> 36 /// 读取Cookie 37 /// </summary> 38 /// <param name="key"></param> 39 /// <returns></returns> 40 public static string GetValue(string cookieName) 41 { 42 string value = HttpContext.Current.Request.Cookies[HttpUtility.UrlEncode(cookieName)].Value; 43 return string.IsNullOrEmpty(value) ? string.Empty : HttpUtility.UrlDecode(value); 44 } 45 46 /// <summary> 47 /// 读取指定Cookie里的指定键的值 48 /// </summary> 49 /// <param name="cookieName">cookie名称</param> 50 /// <param name="keyName">cookie的键</param> 51 /// <returns></returns> 52 public static string GetValue(string cookieName, string keyName) 53 { 54 HttpCookie cookie = HttpContext.Current.Request.Cookies[HttpUtility.UrlEncode(cookieName)]; 55 if (cookie == null) 56 return string.Empty; 57 string value = cookie.Values[HttpUtility.UrlEncode(keyName)]; 58 return string.IsNullOrEmpty(value) ? string.Empty : HttpUtility.UrlDecode(value); 59 } 60 61 /// <summary> 62 /// 获取指定cookie中所有的键 63 /// </summary> 64 /// <param name="cookieName"></param> 65 /// <returns></returns> 66 public static string[] GetValues(string cookieName) 67 { 68 if (HttpContext.Current.Request.Cookies[cookieName] == null) 69 return new string[0]; 70 string[] keys = HttpContext.Current.Request.Cookies[cookieName].Values.AllKeys; 71 for (int i = 0; i < keys.Length; i++) 72 { 73 keys[i] = HttpUtility.UrlDecode(keys[i]); 74 } 75 return keys; 76 } 77 78 /// <summary> 79 /// 移除指定的Cookie 80 /// </summary> 81 /// <param name="cookieName"></param> 82 public static void Remove(string cookieName) 83 { 84 HttpContext.Current.Response.Cookies.Remove(HttpUtility.UrlEncode(cookieName)); 85 HttpContext.Current.Response.Cookies[HttpUtility.UrlEncode(cookieName)].Expires = DateTime.Now.AddDays(-1); 86 } 87 /// <summary> 88 /// 移除cookie中指定的键,若是最后一个键则移除这个cookie 89 /// </summary> 90 /// <param name="cookieName"></param> 91 /// <param name="keyName"></param> 92 public static void Remove(string cookieName, string keyName) 93 { 94 HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; 95 if (cookie != null) 96 { 97 if (cookie.Values.Count > 0) 98 { 99 if (cookie.Values.Count == 1) 100 { 101 //若是最后一个键则移除这个cookie,否则会多出一个空值 102 cookie.Values.Remove(HttpUtility.UrlEncode(keyName)); 103 cookie.Expires = DateTime.Now.AddDays(-1); 104 } 105 else 106 { 107 cookie.Values.Remove(HttpUtility.UrlEncode(keyName)); 108 } 109 HttpContext.Current.Response.Cookies.Add(cookie); 110 } 111 } 112 } 113 }
首先要了解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;
所以
1 cookie.Values.Add(key1, value1); 2 和 3 cookie.Value=cookie.Value+"&key1=value1" 4 是等同的。但是后者在Request.Cookies[cookieName].Values.Count时则会变成1。 5 可能是加上新值之后编码不一所以count的时候变成了1