c# - 使用 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 框架。
任何帮助将不胜感激。谢谢。
解决方案
推荐阅读
- node.js - 我的 node.js socket.io 简单服务器在由 docker run 运行时不起作用
- laravel - Laravel 6 测试 - 未知数据库':内存:'
- sql - 如何确定 VBA SQL 语法问题的原因?
- java - 为什么 Java N1QL 中的 ORDER BY 不能按预期工作
- xamarin.forms - 如何添加单独的项目和类库的初始化
- visual-studio-code - 如何设置 Visual Studio Code 以运行和调试 ColdFusion 代码?
- python - docker中的张量流图像是否使用GPU?
- python - 如何在 scikit-learn 的基本情感分析中使用多个功能?
- xml - 如何从 XML 在 XSD 中声明名称
- python - 如何在我的嵌套字典中打印具有特定值的键?