Cookies not persisted on client side

R

rb

Hi,

This is a very silly (and newbie) question but...

I started a new site; added on default.aspx a text box and a button. On Button Click, I do this:

HttpCookie cookie;
cookie = new HttpCookie( "test", TextBox1.Text );
cookie.Expires.AddDays( 365 );
cookie.Domain = "localhost";
this.Response.Cookies.Add( cookie );

but the cookie won't persist after browser is closed and reopened (IE and FireFox equally).

In Page Load I have

if ( !Page.IsPostBack )
{
if ( Request.Cookies[ "test" ] != null )
TextBox1.Text = Request.Cookies[ "test" ].Value;
}

This "Request.Cookies" is always null and I have absolutely no idea why. I played a bit with web.config but I really don't know what must be set to make cookies persist. My objective is just to save various values and I have no plans to enable authentication once this site is done.

Thanks in advance.

rb
 
G

Guest

AddDays() doesn't alter the Expires value (the new value is a return value).
So, try cookie.Expires = DateTime.Now.AddDays (365);
 
R

rb

Siva M said:
AddDays() doesn't alter the Expires value (the new value is a return
value).
So, try cookie.Expires = DateTime.Now.AddDays (365);

Nicely spotted mistake in my code but it's still a no-go. Cookies still
won't persist.

.... Played a bit more with my code and, apparently, cookie.Domain =
"localhost" was the other part of the problem. When I removed that line, the
cookie finally got saved. I'm not sure why is this the case? Is it because
I'm not redirecting the page or because P3P in follow-up was missing (as
described in thread "Cookies Disappearing") or...

Thanks a lot.

rb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top