为了准备 Chrome 80 中 SameSite 即将发生的更改,我已将 .NET Framework API 从4.6.2
to 4.7.2
.
我创建了一个简单的测试端点,只需设置一个 cookieSameSite=None
:
public class TestController : ApiController
{
public IHttpActionResult Get()
{
var cookie = new HttpCookie("foo", "bar")
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.None
};
HttpContext.Current.Response.SetCookie(cookie);
return Ok();
}
}
这在本地按预期工作,并返回以下标头:
set-cookie: foo=bar; path=/; secure; HttpOnly; SameSite=None
但是,当发布到配置为 4.7 作为运行时堆栈的 Azure Web 应用程序时,这不起作用。 Web 应用程序返回不带 SameSite 的 cookie 标头:
Set-Cookie: foo=bar; path=/; secure; HttpOnly
如果我将其设置为Strict
or Lax
它在 Azure 中也按预期工作。
这是 Azure 的问题吗?是否需要在网络应用程序上配置任何内容才能使其正常工作,或者我可能必须以不同的方式设置 cookie?
From https://stackoverflow.com/a/38957177/1322009 https://stackoverflow.com/a/38957177/1322009。一种也适用于 4.6.1 的解决方案是将以下内容添加到您的 web.config
Edit:Chrome 现在希望您包含secure;
使用 SameSite=none 时在您的 cookie 上。
<system.webServer>
<rewrite>
<outboundRules>
<clear />
<rule name="Add SameSite" preCondition="No SameSite">
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; secure; SameSite=none" />
<conditions>
</conditions>
</rule>
<preConditions>
<preCondition name="No SameSite">
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; secure; SameSite=none" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
这假设您在托管自己的 iis 站点时安装了 url 重写。https://www.iis.net/downloads/microsoft/url-rewrite https://www.iis.net/downloads/microsoft/url-rewrite
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)