Asp – Cookies NULL On Some ASP.NET Pages (even though it IS there!)

asp.netcookies

I'm working on an ASP.NET application and I'm having difficulty in understanding why a cookie appears to be null.

On one page (results.aspx) I create a cookie, adding entries every time the user clicks a checkbox. When the user clicks a button, they're taken to another page (graph.aspx) where the contents of that cookie is read.

The problem is that the cookie doesn't seem to exist on graph.aspx. The following code returns null:

Request.Cookies["MyCookie"];

The weird thing is this is only an issue on our staging server. This app is deployed to a production server and it's fine. It also works perfectly locally.

I've put debug code on both pages:

StringBuilder sb = new StringBuilder();
foreach (string cookie in Request.Cookies.AllKeys)
{
    sb.Append(cookie.ToString() + "<br />");
}

this.divDebugOutput.InnerHtml = sb.ToString();

On results.aspx (where there are no problems), I can see the cookies are:

MyCookie
__utma
__utmb
__utmz
_csoot
_csuid ASP.NET_SessionId
__utmc

On graph.aspx, you can see there is no 'MyCookie'

__utma
__utmb
__utmz
_csoot
_csuid ASP.NET_SessionId
__utmc

With that said, if I take a look with my FireCookie, I can see that the same cookie does in fact exist on BOTH pages! WTF?!?!?!?! (ok, rant over 🙂 )

Has anyone seen something like this before? Why would ASP.NET claim that a cookie is null on one page, and not null on another?

Best Answer

This was happening because I was running the app under a different virtual directory. When I ran it on the original one, it worked.

Related Topic