最近被.NET的HttpCookieCollection搞崩溃了,虽然在Response.Cookies里面可以区分大小写的写入cookie,但是从Request.Cookies获取的时候,根本就不区分大小写啊有没有。。
举个例子:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!HttpContext.Current.Request.Cookies.AllKeys.Contains("account")) 4 { 5 HttpContext.Current.Response.Cookies.Add(new HttpCookie("account", "jason")); 6 HttpContext.Current.Response.Cookies.Add(new HttpCookie("Account", "Jason")); 7 } 8 9 HttpContext.Current.Response.Write(HttpContext.Current.Request.Cookies["Account"].Value); 10 HttpContext.Current.Response.End(); 11 }
上面代码会输出jason,也就是不区分大小写先加入到cookie里面的值。
如何获取到“Account”的值呢?
1 for (int index = HttpContext.Current.Request.Cookies.Keys.Count-1; index >= 0; index--) 2 { 3 string key = HttpContext.Current.Request.Cookies.Keys[index]; 4 if (key == "Account") 5 { 6 return HttpContext.Current.Request.Cookies[index].Value; 7 } 8 } 9 return string.Empty;