1

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?

2 Answers 2

2

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); 
2
  • thanks.... it took me whole day to fix this youre a life saver.. for now i set it to 100 hours will change it later
    – porknbeans
    Dec 20, 2017 at 11:41
  • I am happy for you! Dec 20, 2017 at 12:08
1

You haven't provided a great variety of information so here's my guesses based on what I can think of.

Check your IIS settings

Under your IIS settings, head to your website deployment then under the features panel, navigate to IIS > Authentication > Check you have the correct authentication methods in place. You could also verify that you are allowing all users to authenticate via cookie authentication

Web.config must allow IIS to send cookies to you

If your web.config file is not set up correctly, it may be preventing your deployment from sending cookies. Read through and check that it allows IIS to send them out.

Try adding a single day like so

If you don't receive a cookie, it could be that the cookie is instantly deleted? It's rare, but it happens. Try doing this instead:

ds_id.Expires = DateTime.Now.AddDays(1d);

This should make the cookie persist

Check the server's datetime is correct

If it isn't this could very well be the root of the issue. It sounds silly, but it has been a legitimate problem in the past for some.

Make sure your browser accepts the cookie

If you have any options in your browser to reject cookies, this will prevent you from accepting the login and therefore it will be almost as if you can't log in.

If you still have problems after these fixes, please let us know and provide a bit more detail and we might be able to help narrow down the issue for you.


As an additional note, this is a much better (and cleaner) way of doing this:

var dsCookie = new HttpCookie("dsCookie")
{
    Value = reqCookie.ToString(),
    Expires = DateTime.Now.AddHours(1)
};

Response.Cookies.Add(dsCookie);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.