1

I have the following code:

protected void Page_Load(object sender, EventArgs e)
{
        if (Request.Cookies["MyTestCookie"] == null)
        {
            string str = "Some String";
            HttpCookie myCookie = new HttpCookie("MyTestCookie")
            {
                Value = str,
                Expires = DateTime.Now.AddYears(50)
            };
            HttpContext.Current.Response.SetCookie(myCookie);
        }
 }

But it works until I close my browser. If I close my browser and compile and run my project again the code inside the if block will executes again. So my question is why my cookies expiration doesn't work and the cookie is expiring after I close the browser?

Some notes:

  1. This is in development and localhost and my port is not changed during several running.
  2. I've checked my browser settings and it doesn't deletes cookies upon close.
  • Does the domain remain the same? So not for example a random port number if this is in development? – James Gaunt Nov 10 '16 at 19:23
  • @JamesGaunt Yes this is in development and my port is not changed during several running. –  Nov 10 '16 at 19:28
  • Check your browser settings to see if your browser deletes cookies upon close. – scrappedcola Nov 10 '16 at 19:30
  • Hmmm, so not sure, but maybe it's just the browser doesn't remember cookies in localhost. Can you try in a 'real' domain? See this http://stackoverflow.com/questions/7346919/chrome-localhost-cookie-not-being-set – James Gaunt Nov 10 '16 at 19:30

0 Answers0