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?
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.