首页 > 解决方案 > 使用 ASP.NET 框架 4.6.1 和 Chrome 时未将 Cookie 设置到已部署的网站中

问题描述

Chrome 中未设置 Httpcookie。它在 Mozilla 浏览器中按预期工作。我们正在使用 .NET 框架 4.6.1。

我们在 中编写了以下重写规则web.config

<rewrite>
  <outboundRules>
    <rule name="Add SameSite" preCondition="Lax SameSite">
      <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
      <action type="Rewrite" value="{R:0}; SameSite=Lax" />
      <conditions>
      </conditions>
    </rule>
    <preConditions>
      <preCondition name="Lax SameSite">
        <add input="{RESPONSE_Set_Cookie}" pattern="." />
        <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=Lax" negate="true" />
      </preCondition>
    </preConditions>
  </outboundRules>
</rewrite>

我们正在使用以下代码来设置 cookie

public void Set(string key, string value, int expireHours = 1)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies[Constants.CookieName];

    if (cookie == null)
        cookie = new HttpCookie(Constants.CookieName);

    cookie.Secure = true;
    cookie.Expires = DateTime.Now.AddHours(expireHours);
    cookie[key] = value;

    HttpContext.Current.Response.Cookies.Add(cookie);
}

但是,如果我将本地主机的 Secure 属性设置为 false,它工作正常。由于我们的网站托管在 HTTPS 上,所以我添加了 cookie.Secure = true; 但是,这不适用于 chrome 浏览器。我们现在无法更新 ASP.NET 框架。

任何帮助将不胜感激。谢谢。

标签: c#asp.net-mvcgoogle-chromehttpcookie

解决方案


推荐阅读