23

This morning I accidentally saw the following snippet code, I was fairly surprised because it work very well.

Don't look at its logic please, I'm just curious why does the HttpCookieCollection (Request.Cookies in this case) return a string (cookie name) instead of a HttpCookie object in foreach loop. Is it a consistency issue because we normally get HttpCookie object in this collection by index/name?

Thanks,

foreach (string cookieKey in System.Web.HttpContext.Current.Request.Cookies)
{
    HttpCookie tmpCookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
    if (tmpCookie != null && tmpCookie["RecentlyVisited"] != null)
    {
       cookie.Add(tmpCookie);
    }
}
Nifle
  • 11,285
  • 9
  • 70
  • 93
Tien Do
  • 7,976
  • 5
  • 35
  • 41
  • Thank you, @Chris: I don't ask how to iterate a collection by for loop ;) – Tien Do Jun 22 '09 at 02:49
  • 1
    i have the same issue, i do not understand why do i have to use string insted of HttpCookie in the foreach declaration. any clue? – acromm Sep 18 '09 at 20:41
  • 1
    if using System.Net.CookieCollection you can iterate that way. but not with HttpCookieCollection, strange behavior i think. public static HttpCookieCollection CookieCollectionToHttpCookieCollection(CookieCollection cookieCollection) { HttpCookieCollection httpCookieCollection = new HttpCookieCollection(); foreach (Cookie cookie in cookieCollection) { httpCookieCollection.Add(CookieToHttpCookie(cookie)); } return httpCookieCollection; } – acromm Sep 18 '09 at 20:45

3 Answers3

9

It makes more sense to iterate through a collection by the keys. That way you have access to both the keys and can easily access the value by calling System.Web.HttpContext.Current.Request.Cookies[cookieKey];

HaveNoDisplayName
  • 7,711
  • 106
  • 32
  • 44
Charles Ma
  • 41,485
  • 21
  • 80
  • 98
  • 11
    Not if you have two cookies with the same name in which case this approach is flawed. – Ian Mercer Jul 24 '13 at 18:51
  • As far as I know you can't have two cookies with the same name in one website domain (the only way would be to follow the bad practice to run multiple applications on the same domain with different paths). – needfulthing Jan 11 '21 at 15:36
9

You may want to loop through your cookies by index:

HttpCookieCollection MyCookieColl;
HttpCookie MyCookie;

MyCookieColl = Request.Cookies;

// Capture all cookie names into a string array.
String[] arr1 = MyCookieColl.AllKeys;

// Grab individual cookie objects by cookie name.
for (int i = 0; i < arr1.Length; i++) 
{
   MyCookie = MyCookieColl[arr1[i]];
   Debug.WriteLine("Cookie: " + MyCookie.Name);
   Debug.WriteLine("Expires: " + MyCookie.Expires);
   Debug.WriteLine("Secure:" + MyCookie.Secure);
}
Chris Thompson
  • 14,633
  • 9
  • 40
  • 57
5

Since you can get cookies by their numerical index as well it's actually possible to scan multiple cookies with the same name without having to copy to a CookieCollection or something like that.

This should do the trick:

var cookieName = "yourcookie";
var matches = cookies.AllKeys
    .Select((name, i) => new {name, i})
    .Where(x => x.name == cookieName)
    .Select(x => DoSomethingWithEachMatch(cookies[x.i]));
aclemmensen
  • 51
  • 1
  • 1