asp.net - 如何在 ASP.NET 中将 SameSite cookie 属性减少回无?

标签 asp.net csrf samesite

为了避免 CSRF(跨站点请求伪造),大多数浏览器(自 2019 年末以来)自动考虑任何未明确定义 SameSite 属性的 cookie 将被视为 Lax,而不是之前的默认值 None。

最近(2020 年 2 月,自 Chrome 80 起)浏览器也忽略了定义 SameSite=None 且不安全的 cookie。

如何在我的 Web.config 中更改我的 session cookie 以自动更改为无(以保持我的 SSO 集成工作)?

最佳答案

编辑 2020-08-03 :Chrome 85 doesn't allow insecure SameSite=None cookies
我已经相应地更新了代码:1) 仅适用 SameSite=None如果连接是 https; 2) 只申请 Secure;如果连接是 https; 3) 删除 SameSite=None如果是 http 并且属性添加了 samesite(重写规则)
原回复 :
这是一个纯 web.config 解决方案,它:

  • 定义应使用 SameSite=None 呈现 session cookie
  • 附加 SameSite=None到任何未明确定义 SameSite 属性的 cookie(使用适用于所有框架版本的方法,在最坏的情况下,如果某些属性不被接受,您可以将其删除)
  • 附加 Secure归因于任何尚不安全的 cookie(只要它是 https 请求)
  • 已删除 SameSite=None如果它被以前的规则应用并且由于缺少 https
  • 而无效

    <!-- 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>
    
    
    如果您不使用 <sessionState cookieSameSite="None" />一些较新的 ASP.NET Framework 版本将默认呈现 SameSite=Lax .如果您只是将重写规则添加到 SameSite=None对于所有 cookie,您将获得两次 SameSite 属性,根据我的测试,该属性在 Chrome 和 Firefox 等某些浏览器中有效(将使用 最后 出现 SameSite 属性)但在某些浏览器中不起作用Edge(使用 第一个 属性出现)。
    由于第一个标签 <sessionState cookieSameSite="None" />会自动设置 SameSite=None但不会自动添加 Secure属性,我已经配置了 SameSite=NoneSecure作为独立的规则。如果我将所有内容都放在一个规则中,我最终会得到重复的属性 SameSite=None ,这可能会破坏浏览器(如上所述,它是无效的,浏览器可能会不一致地处理此问题)。Secure仅当它是 HTTPS 请求时才添加,因此如果您仍然接受 HTTP 连接,您的 session cookie 将不会有 Secure添加,这将使浏览器忽略您的 cookie(并且 session 根本不起作用)。如果您使用负载均衡器或反向代理,则应使用 HTTP_X_FORWARDED_PROTO attribute
    最后,some versions of Safari 中有一个错误其中浏览器不理解 SameSite=None并将其视为 SameSite=Strict .因此,对于那些特定版本,您可能决定不渲染 SameSite=None , 虽然如果没有指定默认值仍然是 SameSite=Lax级别,这可能不是您需要的(尚未找到解决方案)。

    关于asp.net - 如何在 ASP.NET 中将 SameSite cookie 属性减少回无?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60117726/

    相关文章:

    php - 如何让 Codeigniter CSRF 保护与 SSL 一起正常工作?

    google-chrome - SameSite cookie、框架、子域和重定向

    c# - 用于仅选择 jquery 列表中已选中项目的选择器

    c# - 使用 NameValueCollection 将 QueryString 转换为 URL-Encode

    asp.net - 如何通过弹出框要求用户确认操作?

    angular - DOM 异常 : Failed to read the 'localStorage' property from 'Window' : In Chrome Incognito mode and running in Iframe

    c# - 在 ASP.NET Core 3.1 Web 应用程序中获取 "Unrecognized SameSiteMode value -1"InvalidOperationException

    ASP.NET Web API knockout 验证

    kohana - CSRF,怎么煮呢?

    javascript - CSRF 漏洞/cookie 问题