Tech Off Thread

9 posts

Cookie Question

Back to Forum: Tech Off
  • User profile image
    odujosh

    In some button click event:
    foreach
    (HttpCookie cookie in Request.Cookies)

    {

    cookie.Expires = DateTime.Now;

    }

    Say I am in PST as a client and the server is EST. Would my cookies expire now (percievable on next request) or in 3 hours?

  • User profile image
    ZippyV

    Pick a date that's years before Now().

  • User profile image
    PeterF

    The time in the cookie would be in GMT, so you also wouldn't have the issue with time zones or winter time, but if you really want to expire it (time on server and time on client can be slightly off) expire it with:

    cookie.Expires = DateTime.Now.AddYears(-1);

  • User profile image
    odujosh

    PeterF wrote:
    The time in the cookie would be in GMT, so you also wouldn't have the issue with time zones or winter time, but if you really want to expire it (time on server and time on client can be slightly off) expire it with:

    cookie.Expires = DateTime.Now.AddYears(-1);


    How would you figure out time zone of DateTime. How to adjust for GMT? I am curious about a more precise answer if anyone has one.

  • User profile image
    longnight


    The help text indicates that what you have should do it.Wink

    http://msdn2.microsoft.com/en-us/library/system.web.httpcookie.expires.aspx

  • User profile image
    odujosh

    Yeah it seems to be a situation of do what I say not as I do (from documentation):

    Property Value

    The time of day (on the client) at which the cookie expires.


    Then they turn around show:

    DateTime dt = DateTime.Now;
    TimeSpan ts = new TimeSpan(0,0,10,0);

    MyCookie.Expires = dt.Add(ts);


    DateTime.Now being the time on the server. So the right hand isn't talking to the left hand:)

    Without building a TimeZone property into the user profile or defaulting to some timezone how do you figure out the clients timezone. Is there some javascript trick I should be aware of Tongue Out

    So which is right? The text or the code?

  • User profile image
    Jorgie

    The header is actually sent as a text representation of the date and for the cookie to be valid, it must be in the form:

    DAY, DD-MMM-YYYY HH:MM:SS GMT

    Since the function you are using takes care of the conversion from a DateTime object to this text, I would hope it would to the conversion for you.

    I will be testing it as soon as I get VWD Express 2008 installed. Smiley

  • User profile image
    PeterF

    odujosh wrote:
    
    How would you figure out time zone of DateTime. How to adjust for GMT? I am curious about a more precise answer if anyone has one.


    That's the thing, by expressing the DateTime object in GMT time, you completely avoid things like time zone and summer and winter time.

    GMT time is completely neutral in that sense, and the DateTime format used by the cookie object takes advantage of that.

    The only thing is, because the cookie object lifetime is not expressed in Time To Live (TTL), you always have a descrepancy between server time and client time. You can try to compensate it by synchronizing things, but you always have the issue of how long a transaction will take or the execution time or the transport time.

    In that sense try to avoid expressing this explicitely yourself in the form of cookies, but use the session timeout mechanisms in stead provided by .NET and/or IIS

    Good luck,
    Peter

  • User profile image
    longnight

    odujosh,

    I just ran a test that shows that the conversion is made from server time zone to client time zone. However, if the client time is off by a few minutes, then the cookie can hang around for a few, or die a few minutes too soon. The source code for the test is in the Sandbox.

    For absolute accuracy, you would need nab the client time and time zone, then convert the client time to the server time zone.

    gl...

    let me know if you nab the client time and time zone.

    btw, you could just store the server time in a cookie and check for expiration yourself.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.