Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Asp.net getting Cookie expiration time set in javascript always returns 01.01.0001

I use a javascript function to store the cookie:

createCookie("teaser", "teaser", 7);
function createCookie(name, value, days) {
var expires = "";
if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
return value;
}

And when I check the cookies in my browser the cookie expiration of teaser is correctly set: 25. oktober 2011 16:12:17

But when in C# i go to get the value, the expiration date is set to 01.01.0001.

    var cookie = Request.Cookies["teaser"];
    if (cookie != null && teaserList.Count() > 0)
    {

        cookie.Expires is 01.01.0001

Any clue?

like image 486
Attila Avatar asked Oct 18 '11 14:10

Attila


1 Answers

Egghead says that:

The browser is responsible for managing cookies, and the cookie's expiration time and date help the browser manage its store of cookies. Therefore, although you can read the name and value of a cookie, you cannot read the cookie's expiration date and time. When the browser sends cookie information to the server, the browser does not include the expiration information. (The cookie's Expires property always returns a date-time value of zero.) If you are concerned about the expiration date of a cookie, you must reset it.

like image 183
Ahmet Kakıcı Avatar answered Oct 05 '22 18:10

Ahmet Kakıcı