【问题标题】:cookie not being set when Expires less than 1 year, on localhost (Chrome and FF)在 localhost(Chrome 和 FF)上过期少于 1 年时未设置 cookie
【发布时间】:2016-05-05 10:46:13
【问题描述】:

我在 localhost 和 ChromeFirefox 中遇到问题,而不是 Edge

这段代码可以正常工作,并且会设置 cookie:

[HttpPost]
public ActionResult Change(string val)
{
    var cookie = new HttpCookie(CookieName) { Value = val, Expires = DateTime.Now.AddYears(1) };

    Response.Cookies.Add(cookie);

    return Content("");
}

但是,如果我将 Expires 更改为仅 300 days,则 cookie 将不会在 Request Cookies 中发回,(对于 Change 请求,它仍将在 Response Cookies 中可见)

编辑: 此操作通过 jquery ajax ($.post) 调用;而且它曾经在半年前工作过

【问题讨论】:

  • 这很奇怪,对我来说很好用: [HttpPost] public ActionResult Change(string val) { var cookie = new HttpCookie("myCookie") { Value = val, Expires = DateTime.Now.AddDays (300) }; Response.Cookies.Add(cookie); return Content(" a "); } 公共 ActionResult Check() { var cookies = Request.Cookies;返回视图(); }
  • 所以即使使用简单的 mvc 应用程序,仅使用上述方法,您也可以始终如一地重现它?还是只做一些比这更复杂的事情?
  • @Evk 是的,在本地主机上的 2 个项目中,这对我来说是相同的行为;虽然在边缘工作

标签: asp.net asp.net-mvc google-chrome firefox asp.net-mvc-5


【解决方案1】:

您是否尝试将 cookie.Domain = null 放在 localhost 上,否则使用域名。

【讨论】:

    猜你喜欢
    • 2011-11-12
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2017-07-21
    • 2016-02-07
    • 1970-01-01
    • 2011-11-03
    相关资源
    最近更新 更多