views:

168

answers:

1

Hello,


The following code creates 100 cookies and then enumerates through them via postbacks.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        for (int i = 0; i < 100; i++)
        {
            HttpCookie cookie = new HttpCookie("cookie num." + i.ToString());
            cookie["Cookie"] = i.ToString() + "th cookie, anyone?";
            cookie.Expires = DateTime.Now.AddMonths(1);
            Response.Cookies.Add(cookie);
        }

        int i1 = 0;
        Session["i"] = i1;
    }

    if (IsPostBack)
    {
        int i = (int)Session["i"];
        HttpCookie cookie= Request.Cookies["Cookie num." + i.ToString()];
        Label1.Text = cookie["Cookie"];
        Session["i"] = ++i;
    }
 }


Q1 - But when using Firefox, instead of being able to enumerate through all of 100 cookies, app only manages to go through first 20 (max 40) cookies before reporting Object reference not set to an instance of an object exeception:


Q2 - when I request this page via IE, then I will always get Object reference not set to an instance of an object exception ( BTW – I checked and IE did manage to store all 100 cookies into a single file ) on the first postback.


thanx


EDIT:

But the following link applies to IE, while it is Firefox that doesn't save all cookies. And with IE, code is not able to enumerate through none of the cookies. SO the above answer would be valid if I was at least able to enumerate through first 20 cookies ( in IE ), which I'm not


2. EDIT:

The spec makes no guarantee which cookies a browser must keep when it runs out of storage, so browsers are free to behave how they like, and you should expect different behaviours. What's more, in your example, all the cookies are set in headers of a single response. HTTP headers are not ordered, so there's no guarantee they will be processed in any particular order in the first place.

What exactly do you mean by "processed"?

thanx for helping me out

+12  A: 

You can't rely on being able to set more than 20 cookies per hostname.

ETA:

But the following link applies to IE, while it is Firefox that doesn't save all cookies.

Yes, but the article quotes RFC2109, which also applies to Firefox.

As for when you get the error, that will depend on which cookies the browser decides to keep. If it keeps the first 20 cookies set, you'll get an error at cookie20; if it throws away the earlier cookies to make room for the new ones, you'll be left with only cookie80-cookie99 and the attempt to access cookie0 will error.

The spec makes no guarantee which cookies a browser must keep when it runs out of storage, so browsers are free to behave how they like, and you should expect different behaviours. What's more, in your example, all the cookies are set in headers of a single response. HTTP headers are not ordered, so there's no guarantee they will be processed in any particular order in the first place.

RFC2109's limits generally don't totally reflect reality, but each browser has its own cookie limits and you should in general strive to keep cookie data down to a bare minimum.

bobince