I'm seeing something of an oddity when setting a cookie...
Action:
string cookieName = "foo";
string cookieValue = "bar";
//Set a cookie in the response, along with the Expires.
this.ControllerContext.HttpContext.Response.Cookies.Add(
new HttpCookie(cookieName, cookieValue)
{
Expires = DateTime.Now.AddHours(1)
}
);
When debugging, I can see that this new cookie has an expiry of one hour in the future, and yet, when I look at the cookie in the view, the expiry isn't there...
View:
<%= Request.Cookies.Get("foo").Value %>
Returns bar
.
<%= Request.Cookies.Get("foo").Expires %>
Returns 01/01/0001 00:00:00
Any ideas?!
You're looking at the request - which doesn't contain an expiry time. The server tells the client when the cookie should expire; there's no need for the client to tell the server as well :)
Response.Cookies
is a very different thing from Request.Cookies
.
Two things: First, if you are looking at the Request before the Response has been pushed to the client, then the Request will not have your updates.
Second, if you are setting a cookie and then using a Response.Redirect, your cookie values might not have been pushed to the client. Under the covers Response.Redirect calls "Thread.Abort()", which is kills the thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With