Pregunta

I am getting crazy with Cookie Expire date, conceptually it is pretty simple but it is not working on my server... I guess that's an IIS setting that's making me crazy, what do you think????

I started with an MVC 3 sample project and I am adding the following code in the Home Controller

  public ActionResult Index()
    {
         var PersistentLogonCookieName = "test";
        var persistentLogon = DateTime.Now.ToLongTimeString(); 
         var persistentLogonCookie = new HttpCookie(PersistentLogonCookieName, persistentLogon);
           persistentLogonCookie.Expires = DateTime.UtcNow.AddDays(2);


        if (Request.Cookies[PersistentLogonCookieName] == null)
        {
            ViewBag.Message = "Cookie NOT Found!";

             persistentLogonCookie.Value = "added" + DateTime.Now.ToLongTimeString();
            Response.Cookies.Add(persistentLogonCookie);
        }
        else
        {
            ViewBag.Message = "Cookie Found!";

            Response.SetCookie(persistentLogonCookie);
        }
        return View();
    }

if I run it locally, it works fine after the second request I got Cookie Found.... if I run on the server I got always Cookie Not Found.... If I comment persistentLogonCookie.Expires = DateTime.UtcNow.AddDays(2); it works fine also on the server. I don't know what to think about if not that's a different IIS settings between my local machine and the server.... In the response header I have:

Set-Cookie  test=added4:23:39 PM; expires=Wed, 14-Aug-2013 15:23:39 GMT; path=/

but the cookie is not added to the cookie collection of following requests when I test it on the server and it goes in the cookie collection when I test with my local IIS.... Any suggestion would be highly appreciated....

¿Fue útil?

Solución

Sorry I found out what was the problem.... the server was returning the wrong DATE in the Response DATE.... The Expiry Date has to be AFTER the Response Date to add the cookie to the cookie collection.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top