Why does Request.Cookies return string instead of HttpCookie object in foreach loop?
Asked Answered
N

3

26

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);
    }
}
Nuri answered 20/6, 2009 at 7:51 Comment(4)
Thank you, @Chris: I don't ask how to iterate a collection by for loop ;)Nuri
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?Rousseau
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; }Rousseau
NB: This may give odd results if you have multiple cookies with the same name, it's more reliable to use the collection indexBiestings
J
10

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];

Jeri answered 20/6, 2009 at 7:57 Comment(3)
Not if you have two cookies with the same name in which case this approach is flawed.Urias
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).Velure
This can too happen #76080645Punchinello
A
10

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);
}
Anthesis answered 20/6, 2009 at 8:7 Comment(0)
F
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]));
Flashcube answered 13/11, 2013 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.