<!-- This sets ASP.NET_SessionId cookie to SameSite=None,
avoiding the default of current frameworks which is LAX -->
<system.web>
<!-- in newer framework versions you have to change the samesite
level like by changing this default level -->
<!-- in old versions these attributes might not be allowed
and in case (if they don't work) just ignore/skip them -->
<sessionState cookieSameSite="None" />
<httpCookies sameSite="None"/>
<authentication mode="Forms">
<forms ..... cookieSameSite="None" />
</authentication>
...
</system.web>
...
<system.webServer>
<rewrite>
<outboundRules>
<!-- for old versions the only solution is to intercept/modify cookies -->
<!-- Add "SameSite=None" to any cookie which does NOT have it yet -->
<!-- currently this only works for secure https cookies -->
<rule name="Add SameSite">
<conditions>
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=None" negate="true" />
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; SameSite=None" />
</rule>
<!-- Add "Secure" to any cookie which does NOT have it yet, as long as it's HTTPS request or else a secure cookie would just be ignored -->
<rule name="Add Secure">
<conditions>
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; Secure" negate="true" />
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; Secure" />
</rule>
<!-- If samesite was set to none by cookieSameSite="None", -->
<!-- remove it for non-https requests (currently only works for https) -->
<rule name="No SameSite For HTTP">
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<match serverVariable="RESPONSE_Set_Cookie" pattern="(.*);(\s*)SameSite=None" />
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
1条答案
按热度按时间vlju58qv1#
2020年8月3日编辑:Chrome 85 doesn't allow insecure SameSite=None cookies
我已经相应地更新了代码:1)如果连接是https,则仅应用
SameSite=None
; 2)如果连接是https,则仅应用Secure;
; 3)删除SameSite=None
,如果它是http,并且属性添加了samesite(重写规则)原答复:
这是一个纯粹的web.config解决方案,它:
SameSite=None
呈现会话CookieSameSite=None
附加到任何没有显式定义SameSite属性的cookie(使用在所有框架版本中工作的方法,在最坏的情况下,如果某些属性不被接受,您可以删除它)Secure
属性附加到任何尚不安全的cookie(只要是https请求)SameSite=None
(如果之前的规则应用了它,并且由于缺少https而无效)如果不使用
<sessionState cookieSameSite="None" />
,一些较新的ASP.NET Framework版本将默认呈现SameSite=Lax
。如果您刚刚使用重写规则将SameSite=None
添加到所有cookie中,您将获得两次SameSite属性,根据我的测试,它可以在Chrome和Firefox等浏览器中运行(它将使用SameSite属性的last匹配项),但在Edge(它使用属性的first匹配项)中不起作用。由于第一个标签
<sessionState cookieSameSite="None" />
会自动设置SameSite=None
,但不会自动添加Secure
属性,因此我将SameSite=None
和Secure
配置为独立的规则。如果我将其全部包含在一个规则中,最终会得到重复的属性SameSite=None
。这可能会破坏浏览器(如上所述,它是无效的,浏览器可能会处理不一致)。Secure
仅在HTTPS请求时添加,因此如果您仍然接受HTTP连接,您的会话cookie将不会添加Secure
,这将使浏览器忽略您的cookie(并且会话根本无法工作)。最后,some versions of Safari中有一个bug,浏览器无法理解
SameSite=None
并将其视为SameSite=Strict
。因此,对于那些特定版本,您可能决定不渲染SameSite=None
,尽管如果未指定,默认值仍为SameSite=Lax
级别,这可能不是您所需要的(尚未找到解决方案)。