I’m trying to create a persistent cookie. Here is the code:
string expires = string.Format("{0} GMT", DateTime.Now.AddHours(1).ToString("ddd, dd-MMM-yyyy HH:mm:ss"));
string newCookie = string.Format("Cookie1=ValueToStore; expires={0}", expires);
Uri location = new Uri("http://www.mysite.com/Application");
System.Windows.Application.SetCookie(location, newCookie);
string theSetCookie = System.Windows.Application.GetCookie(location);
The string theSetCookie should contain all the data in the cookie, no? However it only contains Cookie1=ValueToStore;. What am I missing? Why didn’t the expiration date get in and make it a persistent cookie?
Note: I am using Internet Explorer 9 in Windows 7
The expiration timestamp isn’t part of the Cookie value. When you set the cookie you set it along with some given parameters (Expires), but when you read it, you only read the value.
The cookie expiration shouldn’t drive any logic in your application:
None of these scenarios require the expiration timestamp to be known.