views:

2242

answers:

3

I have a script that reads a form and puts some info into a cookie:

Dim oCookie as HttpCookie
oCookie = New HttpCookie("authInfo")
Select Case oResult
Case "No ClientID", "No Password", "No PracType", "No Encrypt", "CRC Mismatch"
    oCookie.Values.Add("LoggedIn", "False")
    oCookie.Values.Add("OnSupport", "False")
Case "Client Can Update"
    oCookie.Values.Add("LoggedIn", "True")
    oCookie.Values.Add("OnSupport", "True")
Case "Client Cannot Update"
    oCookie.Values.Add("LoggedIn", "True")
    oCookie.Values.Add("OnSupport", "False")
End Select
oCookie.Expires = DateTime.Now.AddHours(2)
HttpContext.Current.Response.Cookies.Add(oCookie)
HttpContext.Current.Response.Redirect("default.aspx")

The time is properly set right before it redirects, but when I try to print out that value this way (I have also tried Dim oCol as HttpCookieCollection = Request.Cookies, but I get the same result):

Response.Output.WriteLine(Request.Cookies("authInfo").Expires.ToString)

The time is always reset "01/01/0001 12:00:00 AM". Am I missing something that prevents the cookie from holding its Expires value?

+7  A: 

Request.Cookies and Response.Cookies and use of the common cookie object makes of an intuative idiom but in fact a Request cookie and a Response cooke are quite different.

When you assign a Cookie in the response a Set-Cookie header gets added to the output and that will contain not only the value but also the path and the expiries value.

However when the browser sends the cookie back to the server in another request it simply includes the cookie name and value. It does not send path or expires info.

Hence these properties are meaningless when using the Request.Cookies collection.

AnthonyWJones
So how, if it is possible, do I preserve the Expires value?
Anders
@Anders: are you changing the cookie value? If so why would it not get a new expires value? If not why are you trying to set it again, you need only create the cookie once.
AnthonyWJones
No, check the code above. I am creating a new cookie, setting 3 values (including the Expires; 2 otherwise).
Anders
@Anders: The browser will preserve it and will terminate the cookie once it has expired. Do you need to read the expires value in a subsequent request? If you so you will need store it in some other persistant storage serverside.
AnthonyWJones
You don't need to preserve the value. It staysconstant until hte cookie expires. Also FYI you really shouldn't be passing sql queries around in cookies. Execute the sql and get the value and store that in a cookie, but not the query itself. It's not that hard for other people to view those.
Chris Westbrook
@ Chris: I am not storing queries in the cookies, where do you see that?
Anders
@Anthony: I think I'll encode a string containing all the values I need then pass that along instead of trying to muck around with this issue. Thanks for your insight on how Request/Response cookies work and differ.
Anders
Never mind, I've been using sql too long and saw select case and thought that was an sql statement. Geeze I need to go home. :)
Chris Westbrook
A: 

I ran into such an issue when the initial request came from a different domain.
make sure you also add the domain for the cookie.

Dror
I tried that; no avail. I am testing on localhost right now, do I need include the port as part of the domain (ie localhost:12345 instead of just localhost)?
Anders
Nevermind, that did not work either.
Anders
A: 

If you want to check for the expiration date you can add it to the cookie value collection. Then retrieve it and do the check.

Gargamel