views:

1235

answers:

9

I have the following code that sets a cookie:

  string locale = ((DropDownList)this.LoginUser.FindControl("locale")).SelectedValue;
  HttpCookie cookie = new HttpCookie("localization",locale);
  cookie.Expires= DateTime.Now.AddYears(1);
  Response.Cookies.Set(cookie);

However, when I try to read the cookie, the Value is Null. The cookie exists. I never get past the following if check:

         if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))

Help?

+5  A: 

The check is done after a post back? If so you should read the cookie from the Request collection instead.

The cookies are persisted to the browser by adding them to Response.Cookies and are read back from Request.Cookies.

The cookies added to Response can be read only if the page is on the same request.

Aleris
A: 
aix
A: 

I have tried correcting the code to use Request.Cookies after post back (as shown above), but the problem is the same.

Joda
The browser on the client allows such persistant cookies? Could it be that the client has a more restrictive than usuall policy on cookies?Are you trying to read the cookie in post back to this same ASP.NET page?
AnthonyWJones
A: 

use Response.Cookies.Add(cookie); instead of Response.Cookies.Set(cookie);

Aleris
A: 

Try this snippet -

string locale = ((DropDownList)this.LoginUser.FindControl("locale"))
                                                    .SelectedValue;  
HttpCookie myCookie = new HttpCookie("localization");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("locale", locale);
Response.Cookies["localization"].Expires = DateTime.Now.AddYears(1);

& to read it -

if (Request.Cookies["localization"] != null)
{
   HttpCookie cookie = Request.Cookies["localization"];
   string locale = cookie.Values["locale"].ToString();
}
aix
A: 

if you're compiling in debug mode, turn on tracing for the pages in question and make sure the cookie is in the request collection. Set trace in the @page directive in the aspx file.

Chris Westbrook
A: 

I have this problem as well.

 HttpContext.Current.Response.Cookies["AuthID"].Value = authID;
 HttpContext.Current.Response.Cookies["AuthID"].Expires = DateTime.Today.AddDays(365);

Request.Cookies["AuthID"].Value AND Response.Cookies["AuthID"].Value are always null after a post back. I have looked at the Cookie in FireFox and everything about it is not expired and it has a value.

Also, the cookie IS available in the Response Collection but its value and expiration date are NULL.

Rob
A: 

I had a similar problem, I couldn't read cookies on postback. The issue for me was that I checked the Secure property of the cookie to true. It is said that when the Secure property of the cookie is turned on, it causes the cookie to be transmitted only if the connection uses the Secure Sockets Layer. I am not sure, however, how I was able to see the cookie in the browser the first time, but not on postback, considering that I wasn't transmitting over SSL. Anyhow, turning the cookie.Secure to false, solved the problem, and had cookies read on postback.

Sorry if this doesn't have to do anything with your issue, I wanted to share this, because I spent some time looking how to resolve this.

Fil
A: 

The most likely answer is seen on this post

When you try to check existence of a cookie using Response object rather than Reqest, ASP.net automatically creates a cookie.

Chris Marisic