Search code examples
c#asp.net-mvciiswindows-server-2012-r2httpcookie

Request.Cookie does not work after deploy on IIS


This code below runs on the development environment, but when deployed on Windows Server 2012 R2 the DsCookie cookie could not get the values created from the cookie creation.

It uses MVC 4 with Entity Framework v4 and jQuery.

HttpCookie ds_id = new HttpCookie("ds");
ds_id.Value = reqCookie.ToString();
ds_id.Expires = DateTime.Now.AddHours(1);
Response.SetCookie(ds_id);
                Response.Flush();

private HttpCookie DsCookie
{
    get
    {
        return Request.Cookies["ds"];
    }
}

Does anyone know why my solution only works in a development environment and not live?


Solution

  • May be there is a difference between the server time and your local time.

    The code

     DateTime.Now.AddHours(1); 
    

    takes the server time. And if the server time is 1 hour earlier than your local pc time, the cooke will be created and deleted immediately.

    Check the server time, if this is the case, change the expiration time like

    ds_id.Expires = DateTime.Now.AddHours(10); //Or more. 
    

    Or you can precise time difference and :

      ds_id.Expires = DateTime.Now.AddHours(1 + time difference between the server and your local time);