SameSite 属性如何自动添加到我的 Asp.net_SessionID cookie?

Sad*_*egh 32 asp.net iis samesite

最近samesite=lax 自动添加到我的会话cookie 中!此属性只是添加到 sessionID: "Set-Cookie ASP.NET_SessionId=zana3mklplqwewhwvika2125; path=/; HttpOnly; **SameSite=Lax**"

我的网站托管在 IIS 8.5、Windows 2012 R2 上,没有 WAF 或 UrlRewrite,我关闭了 AntiVirus (kasper)。

但在某些客户服务器上仍然存在同样的问题。

任何的想法?

编辑: 我发现这个:https : //support.microsoft.com/en-us/help/4524419/kb4524419

当 HttpCookie.SameSite 值为“无”时,ASP.NET 现在将发出 SameSite cookie 标头,以适应 Chrome 中 SameSite cookie 处理即将发生的变化。作为此更改的一部分,FormsAuth 和 SessionState cookie 也将使用 SameSite = 'Lax' 而不是先前的默认值 'None' 发出,尽管这些值可以在 web.config 中被覆盖。

如何在 web.config 中覆盖 SessionState 的相同站点 cookie?我添加了这一行,但它不适用于 SessionID cookie! <httpCookies sameSite="Unspecified" />

编辑:我发现这个:https : //docs.microsoft.com/en-us/dotnet/api/system.web.configuration.sessionstatesection.cookiesamesite?view=netframework-4.8#System_Web_Configuration_SessionStateSection_CookieSameSite

通过 SessionState 标签的“cookieSameSite”属性为 stateserver 设置相同站点。

小智 23

将这些选项添加到 web.config 以获取 sameSite=None 、 Lax 或 Strict

<system.web>
    <httpCookies sameSite="None"/>
    <sessionState cookieSameSite="None" />
    <authentication mode="Forms">
        <forms cookieSameSite="None" />
    </authentication>
Run Code Online (Sandbox Code Playgroud)

  • `&lt;httpCookies SameSite` 在 .Net Framework 4.8 上未显示为有效选项 (6认同)
  • @AnkushJain,不,从 .Net Framework 4.7.2 开始就支持它。 (2认同)
  • @IronSean 这是 VS 的抱怨。那只是微软没有更新他们的工具。不过该设置确实有效。 (2认同)

zem*_*ien 21

CookieSameSite attribute is not available for many older frameworks. If you're in the situation where the accepted answer is not supported in your environment, read on!

I modified upon several SO answers to come up with this URL rewrite that adds SameSite=None to session cookies, and also remove SameSite=None from all cookies for most incompatible browsers. The aim of this rewrite is to preserve the "legacy" behaviour pre-Chrome 80.

Full write-up in my Coder Frontline blog:

<rewrite>
  <outboundRules>
    <preConditions>
      <!-- Checks User Agent to identify browsers incompatible with SameSite=None -->
      <preCondition name="IncompatibleWithSameSiteNone" logicalGrouping="MatchAny">
        <add input="{HTTP_USER_AGENT}" pattern="(CPU iPhone OS 12)|(iPad; CPU OS 12)" />
        <add input="{HTTP_USER_AGENT}" pattern="(Chrome/5)|(Chrome/6)" />
        <add input="{HTTP_USER_AGENT}" pattern="( OS X 10_14).*(Version/).*((Safari)|(KHTML, like Gecko)$)" />
      </preCondition>
    </preConditions>

    <!-- Adds or changes SameSite to None for the session cookie -->
    <!-- Note that secure header is also required by Chrome and should not be added here -->
    <rule name="SessionCookieAddNoneHeader">
      <match serverVariable="RESPONSE_Set-Cookie" pattern="(.*ASP.NET_SessionId.*)" />
      <!-- Use this regex if your OS/framework/app adds SameSite=Lax automatically to the end of the cookie -->
      <!-- <match serverVariable="RESPONSE_Set-Cookie" pattern="((.*)(ASP.NET_SessionId)(=.*))(?=SameSite)" /> -->
      <action type="Rewrite" value="{R:1}; SameSite=None" />
    </rule>

    <!-- Removes SameSite=None header from all cookies, for most incompatible browsers -->
    <rule name="CookieRemoveSameSiteNone" preCondition="IncompatibleWithSameSiteNone">
      <match serverVariable="RESPONSE_Set-Cookie" pattern="(.*)(SameSite=None)" />
      <action type="Rewrite" value="{R:1}" />
    </rule>
  </outboundRules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

This should work for most ASP .Net and ASP .Net Core applications, although newer Frameworks have proper code and config options to let you control this behaviour. I would recommend researching all the options available to you before using my rewrite above.

  • 在`&lt;system.webServer&gt;`中 (2认同)
  • @cederlof你是对的!我没有正确测试我的正则表达式,因为我的环境是较旧的 .Net 框架,它不会自动添加 Lax 属性。在您的情况下,您可以使用不同的正则表达式来排除 `SameSite=Lax` 标头: `((.*)(ASP.NET_SessionId)(=.*))(?=SameSite)` 请参阅更新的 https://regex101。 com/r/7D9UdO/3 - 但请注意,此正则表达式将排除您之后可能想要的任何内容,例如安全标头。这应该是一种罕见的情况,因此关键点是查看您的操作系统+框架+应用程序发出的内容并编写适合的正则表达式。我将更新我的答案以反映两种方式:) (2认同)

Sad*_*egh 12

我不能使用重写,因为 UrlRewrite 没有安装在我所有的客户服务器上。

最后,我将 cookieSameSite 添加到我的 web.config:

<sessionState mode="StateServer" cookieSameSite="None" sqlConnectionString="data source=(local);user id=sa;password=" cookieless="false" timeout="20" />
Run Code Online (Sandbox Code Playgroud)


Moh*_*ini 10

最后更新: zemien 的回答比我的更全面和完整。因为它基于用户代理设置 cookie。

我的答案:

您可以通过以下方式在 web.config 中为 ASP.NET_SessionId 替换 SameSite=Lax 为 SameSite=None :

<rewrite>
  <outboundRules>
    <rule name="AddSameSiteCookieFlag">
      <match serverVariable="RESPONSE_Set-Cookie" pattern="((.*)(ASP.NET_SessionId)(=.*))(SameSite=Lax)" />
      <action type="Rewrite" value="{R:1};SameSite=None" />
    </rule>
  </outboundRules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

更新: 为防止IOS问题,更换

<action type="Rewrite" value="{R:1};SameSite=None" />
Run Code Online (Sandbox Code Playgroud)

<action type="Rewrite" value="{R:1};" />
Run Code Online (Sandbox Code Playgroud)

  • 仅当服务器上安装了 IIS 重写模块时才可以实现此操作 (3认同)

C R*_*lph 6

@zemien 您的解决方案正确解决了我们的 google chrome 问题

我们有一个集成,我们的应用程序嵌入到第三方的 iframe 中。2020 年 2 月 4 日发布的 Chrome 80 版阻止了 cookie 的加载。

但是,我必须修改模式以捕获所有 cookie,添加安全标志,以及不为本地非 https 环境在 localhost 上应用重写的条件

<rule name="SessionCookieAddNoneHeader">
      <match serverVariable="RESPONSE_Set-Cookie" pattern="(.*)(SameSite=.*)?" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
      </conditions>
      <action type="Rewrite" value="{R:1}; SameSite=None; Secure" />
</rule>
Run Code Online (Sandbox Code Playgroud)


小智 5

为我工作。添加到我的 web.config 文件中:

<sessionState cookieSameSite="None"></sessionState>
Run Code Online (Sandbox Code Playgroud)

升级到 .Net Framework 4.8 + 安装补丁:2019-12 累积更新 .NET Framework 3.5 和 4.8 for Windows 10 Version 1909 for x64 (KB4533002)