myCookie.Expires = DateTime.MinValue

Discussion in 'ASP .Net' started by Mark, Jan 29, 2007.

  1. Mark

    Mark Guest

    It's my understanding that the code below will create a session cookie (RAM
    based cookie) that does not persist to a file, but exists in memory on the
    client's pc and will be deleted when the user's browser is closed. Correct
    or incorrect? Are there any variations on how this code will behave
    differently between different browsers?
    HttpCookie myCookie = new HttpCookie("my key", "my value");
    myCookie.Expires = DateTime.MinValue;
    HttpContext.Current.Response.Cookies.Add(myCookie);

    Thanks in advance.

    Mark
     
    Mark, Jan 29, 2007
    #1
    1. Advertising

  2. Mark

    Scott M. Guest

    "Mark" <> wrote in message
    news:...
    > It's my understanding that the code below will create a session cookie
    > (RAM based cookie) that does not persist to a file, but exists in memory
    > on the client's pc and will be deleted when the user's browser is closed.
    > Correct or incorrect?


    Well, not quite. A session cookie is a cookie that does not have a date
    indicated for its expiration. You are indicating an expiration date
    (allbeit in the past), so you are creating a permenant cookie and expiring
    it immediately. If you were to remove this line:

    myCookie.Expires = DateTime.MinValue;

    You'd have a session cookie.

    > Are there any variations on how this code will behave differently between
    > different browsers?


    Not really because this code doesn't execute on a browser, it executes on
    the server. The only place where you would have browser to browser
    differences is with different browsers set to accept/reject cookies in the
    first place.

    > HttpCookie myCookie = new HttpCookie("my key", "my value");
    > myCookie.Expires = DateTime.MinValue;
    > HttpContext.Current.Response.Cookies.Add(myCookie);
    >
    > Thanks in advance.
    >
    > Mark
    >
    >
     
    Scott M., Jan 29, 2007
    #2
    1. Advertising

  3. Mark

    clintonG Guest

    Any value greater than 1 will persist on he disk for the period related to
    its Day, Month, Year property. If I recall the rules when we do not use the
    Expires property we get a session cookie and using Expires with a value of 0
    or less will not persist and in fact delete the cookie from disk. Go find
    IECookieView or use Firefox to watch and test cookies.

    <%= Clinton Gallagher
    NET csgallagher AT metromilwaukee.com
    URL http://clintongallagher.metromilwaukee.com/
    MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h


    "Mark" <> wrote in message
    news:...
    > It's my understanding that the code below will create a session cookie
    > (RAM based cookie) that does not persist to a file, but exists in memory
    > on the client's pc and will be deleted when the user's browser is closed.
    > Correct or incorrect? Are there any variations on how this code will
    > behave differently between different browsers?
    > HttpCookie myCookie = new HttpCookie("my key", "my value");
    > myCookie.Expires = DateTime.MinValue;
    > HttpContext.Current.Response.Cookies.Add(myCookie);
    >
    > Thanks in advance.
    >
    > Mark
    >
    >
     
    clintonG, Jan 29, 2007
    #3
  4. Mark

    Mark Guest

    That makes sense conceptually - although walking through my code indicates
    that the DateTime.MinValue is persisting ... somewhere. Would a session
    based cookie be visible through IECookieView? My gut and experiments
    indicate they are not.

    Thanks again.

    Mark


    "clintonG" <> wrote in message
    news:...
    > Any value greater than 1 will persist on he disk for the period related to
    > its Day, Month, Year property. If I recall the rules when we do not use
    > the Expires property we get a session cookie and using Expires with a
    > value of 0 or less will not persist and in fact delete the cookie from
    > disk. Go find IECookieView or use Firefox to watch and test cookies.
    >
    > <%= Clinton Gallagher
    > NET csgallagher AT metromilwaukee.com
    > URL http://clintongallagher.metromilwaukee.com/
    > MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h
    >
    >
    > "Mark" <> wrote in message
    > news:...
    >> It's my understanding that the code below will create a session cookie
    >> (RAM based cookie) that does not persist to a file, but exists in memory
    >> on the client's pc and will be deleted when the user's browser is closed.
    >> Correct or incorrect? Are there any variations on how this code will
    >> behave differently between different browsers?
    >> HttpCookie myCookie = new HttpCookie("my key", "my value");
    >> myCookie.Expires = DateTime.MinValue;
    >> HttpContext.Current.Response.Cookies.Add(myCookie);
    >>
    >> Thanks in advance.
    >>
    >> Mark
    >>
    >>

    >
    >
     
    Mark, Jan 29, 2007
    #4
  5. Mark

    Scott M. Guest

    It depends on how many name/value pairs you have in the cookie itself.

    If you had created 2 name/value pairs of data, such as:

    user=Dave
    id=22

    And, you had set an expiration date (of any value equal to now or higher) on
    either of them, then the cookie would be persisted on the client.

    If you had set expirations on both of the name/value pairs and only expired
    one of them, then the cookie file will still persist, but the expired data
    will not be in it.

    After a cookie is written to the client's hard drive, the ONLY way to remove
    it (from code) is to expire all of the name/value pairs that may be in the
    cookie.

    As I indicated earlier, if you just want session cookies, don't bother
    adding any expiration dates in the first place and the data will only
    persist in the client's memory. The fact that you are setting an expiration
    date (even though it is in the past) causes the cookie file to become a
    persistent cookie, rather than a session cookie.



    "Mark" <> wrote in message
    news:uKXvN5%...
    > That makes sense conceptually - although walking through my code indicates
    > that the DateTime.MinValue is persisting ... somewhere. Would a session
    > based cookie be visible through IECookieView? My gut and experiments
    > indicate they are not.
    >
    > Thanks again.
    >
    > Mark
    >
    >
    > "clintonG" <> wrote in message
    > news:...
    >> Any value greater than 1 will persist on he disk for the period related
    >> to its Day, Month, Year property. If I recall the rules when we do not
    >> use the Expires property we get a session cookie and using Expires with a
    >> value of 0 or less will not persist and in fact delete the cookie from
    >> disk. Go find IECookieView or use Firefox to watch and test cookies.
    >>
    >> <%= Clinton Gallagher
    >> NET csgallagher AT metromilwaukee.com
    >> URL http://clintongallagher.metromilwaukee.com/
    >> MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h
    >>
    >>
    >> "Mark" <> wrote in message
    >> news:...
    >>> It's my understanding that the code below will create a session cookie
    >>> (RAM based cookie) that does not persist to a file, but exists in memory
    >>> on the client's pc and will be deleted when the user's browser is
    >>> closed. Correct or incorrect? Are there any variations on how this code
    >>> will behave differently between different browsers?
    >>> HttpCookie myCookie = new HttpCookie("my key", "my value");
    >>> myCookie.Expires = DateTime.MinValue;
    >>> HttpContext.Current.Response.Cookies.Add(myCookie);
    >>>
    >>> Thanks in advance.
    >>>
    >>> Mark
    >>>
    >>>

    >>
    >>

    >
    >
     
    Scott M., Jan 29, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    3
    Views:
    1,782
    Natty Gur
    Aug 10, 2003
  2. DotNetJunkies User

    datetime=null return minvalue

    DotNetJunkies User, Jun 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    474
    DotNetJunkies User
    Jun 8, 2004
  3. George Jin
    Replies:
    2
    Views:
    3,108
    George Jin
    Mar 17, 2005
  4. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    765
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  5. merrick

    DateTime.MinValue and RangeValidators

    merrick, Oct 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    131
    merrick
    Oct 5, 2005
Loading...

Share This Page