views:

308

answers:

4

IE works fine, but FireFox does not.

I am setting a cookie on my default.aspx page:

    HttpCookie hc = new HttpCookie("guid", guid.ToString());
    hc.Expires = DateTime.Parse("12/12/2010");
    Response.Cookies.Add(hc);

My upload control (I'm using SWFUpload) submits to upload.aspx.

On upload.aspx I call:

    if (Request.Cookies["guid"] != null)
    {
       // Do something.
    }

...my cookie only contains my ASP.NET session variable. Any ideas?

A: 

First off, there's a type-safe constructor for DateTime, which is new DateTime(2010, 12, 12).

Second, you're using different names for your cookie: guid vs applicationGuid. Use either, not both.

Anton Gogolev
Eh, should have been the same name -- that wasn't the issue.
George
A: 

This is what I used to add/get cookie values. Works for me in both IE and FF

addCookie:

HttpCookie c = new HttpCookie("myCookie");
c.Expires = new DateTime(2050, 1, 1);
c.Values.Add("key", "value");

getCookie:

string value = Request.Cookies["myCookie"]["key"];
David
reason for downvote?
David
+1 because I disagreed with the -1. The only other thing you could have added was "We need more information on why your setup is different than this".
Greg
A: 

Behind the scenes, you are probably setting the same cookie twice. Firefox and IE probably differ on which one they choose to keep. ASP.NET likes to set a "guid" cookie automatically in a lot of web applications. By choosing that name, you are bound to create tension between the automatic logic and your own. The best way to see what is happening is to load the Live HTTP Headers add-on to Firefox. It will allow you to see exactly what cookie commands are being sent over to the end-user. You can also force a similar problem to see it recreated:

HttpCookie hc = new HttpCookie("testcookie", "xyz");
hc.Expires = DateTime.Parse("12/12/2010");
Response.Cookies.Add(hc);
hc = new HttpCookie("testcookie", "abc");
Response.Cookies.Add(hc);

This results in an HTTP header with two Set-Cookie calls:

Set-Cookie: testcookie=xyz; expires=Sun, 12-Dec-2010 07:00:00 GMT; path=/
Set-Cookie: testcookie=abc; expires=Sun, 12-Dec-2010 07:00:00 GMT; path=/

From there, it is up to the browser to decide whether first or last is the final value. If two browsers do it differently, you end up with the situation you describe. Install the Live HTTP Headers add-on and look for something similar. At the very least, you should probably consider "guid" to be a cookie name that you should use in an ASP.NET forms app.

If you absolutely need to have multiple places set the same cookie, try to find it first (create a new one if it doesn't exist). This will ensure you are overriding the value of the existing cookie rather than creating another cookie with the same name.

HttpCookie hc = Response.Cookies["testcookie"];
if (null == hc) {
    hc = new HttpCookie("testcookie");
    Response.Cookies.Add(hc);
}
hc.Value = "xyz";
patridge
+1  A: 

I have had the same issue when trying to upload files in Firefox through my Flex application. If you're also using Flash, you may want to do what I did; if you're using the HTML controls, this may or may not apply.

What I did to work around the Firefox issue is issue a one-time use token on the server, then post that token when doing the upload (for example, it could be a hidden field in the form). If the token is recognized upon upload, the upload is processed, then the token is expired. So it's basically what you're doing, only without using a cookie.

Jacob
This is what I ended up doing an implementation of -- I added a parameter to the swfupload control before the page was rendered. This way it was in the header, and not dependent on a cookie/session. Good advice. Thanks.
George
So this has something to do with Adobe even though the initial question doesn't mention it at all?
Greg
I guessed as much since I was seeing this same issue when uploading through Flash. I updated the question so it mentions SWFUpload.
Jacob