I am breaking my head to figure out a browser specific issue (in Firefox and Chrome). I have spent so much time to try fixing this issue that I have finally thought to create a live demo for the experts here to look into this issue. (Hopefully it pays off)
I have two domains www.nkmekal.com and www.incessantcoding.com
Please use Firefox/Chrome to replicate the issue:
Step 1:
Browse http://www.nkmekal.com/createcookie.aspx
The page just creates a cookie. Below is the code that creates the cookie:
// In On_Load of nkmekal.com/createCookie.aspx
HttpCookie cookie = new HttpCookie("DisCookie");
cookie.Value = "djdjd77676ydjdndgdidjkdnhf";
cookie.HttpOnly = true;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
lblCookieInfo.Text = string.Format("<b>Cookie Name:</b> {0} <br/><br/> <b>Cookie Value:</b> {1} <br/><br/> <b>Cookie Expires On:</b> {2}", cookie.Name, cookie.Value, cookie.Expires);
Step 2:
Now open a new tab in the browser, go to http://www.incessantcoding.com/GoTonkmekal.aspx which basically does a simple 302 redirect to http://www.nkmekal.com/ReadCookie.aspx , below is the code that does this redirect:
// In On_Load of incessantcoding.com/GoTonkmekal.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("http://www.nkmekal.com/ReadCookie.aspx");
}
However I see the below message: (Please see the code of ReadCookie.aspx page in Step 3)
“No Cookie Found :(”
Which means that the domain www.nkmekal.com was unable to read the cookie that it created earlier when you’ve browsed www.nkmekal.com/createcookie.aspx
Step 3:
And the page http://www.nkmekal.com/ReadCookie.aspx just tries to read the above created cookie (in Step 1) and displays cookie data. Below is the code that tries to read the cookie and displays it in the page
// In On_Load of nkmekal/ReadCookie.aspx
HttpCookie cookie = Request.Cookies["DisCookie"];
if (cookie != null)
{
// Resetting expiry date because the browser never sends expiry date to Server,
// as cookies expiration dates are irrelevant to servers.
cookie.Expires = DateTime.Now.AddDays(1);
lblCookieInfo.Text = string.Format("<b>Yes! I found a cookie</b> <br><br><b>Cookie Name:</b> {0} <br/><br/> <b>Cookie Value:</b> {1} <br/><br/> <b>Cookie Expires On:</b> {2}", cookie.Name, cookie.Value, cookie.Expires);
}
else
{
lblCookieInfo.Text = "No Cookie Found :(";
}
The above steps work fine only in IE but not in FireFox/Chrome.
Also, if you want to take a peek at the source code of the two domains you can download them at
http://dl.dropbox.com/u/1248159/incessantcoding.zip
http://dl.dropbox.com/u/1248159/nkmekal.zip
Why am I trying to do this:
So, the reason why I am trying to do this is that there are certain operations that I need to perform in the domain www.incessantcoding.com if there was a cookie created in www.nkmekal.com
And the reason for going with a 302 redirect is that we cannot read cross domain cookies and hence I am trying to get the cookies read from the appropriate domain only (since nkmekal.com can only read its cookies).
Any help/suggestions will be very helpful.
Update: Also quite interestingly, if steps 1 and 3 are performed (leaving out step 2), the cookie value is read in Firefox and Chrome correctly. Its only the 302 way that isn't working.