c# Asp.net 获取在 javascript 中设置的 Cookie 过期时间总是返回 01.01.0001

标签 c# javascript asp.net date cookies

我使用 javascript 函数来存储 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;
}

当我检查浏览器中的 cookie 时,teaser 的 cookie 过期设置正确: 25. 2011 年 10 月 16:12:17

但是当我在 C# 中获取值时,到期日期设置为 01.01.0001。

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

        cookie.Expires is 01.01.0001

有什么线索吗?

最佳答案

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.

关于c# Asp.net 获取在 javascript 中设置的 Cookie 过期时间总是返回 01.01.0001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809072/

相关文章:

c# - Microsoft Access 数据库引擎找不到对象“Sheet1$”

javascript - 如何测试渲染时间(不是 DomContentLoaded 也不是 onLoad)?

c# - Web API中多个过滤器的执行顺序

asp.net - 无法向 session 状态服务器发出 session 状态请求

c# - 当我设置 EnableSSL= false 时出现错误

C#图表垂直文本注释

c# - Cosmos DB - ExecuteNextAsync 返回空对象

javascript - 在 javascript 中减少嵌套的 "if' s"

javascript - 单击链接将暂停动画播放状态更改为运行

c# - 如何限制用户可以在文本框中输入的字符数?