【问题标题】:IIS outbound rewite rule with condition based on the request (web.config)具有基于请求的条件的 IIS 出站重写规则 (web.config)
【发布时间】:2020-01-17 08:11:45
【问题描述】:

我目前遇到的问题是,在 Windows Server 上更新 .Net Framework (https://support.microsoft.com/en-us/help/4524419/kb4524419) 后,IIS 使用 sameSite=lax 属性为我的所有 cookie 提供服务

问题类似于how SameSite attribute added to my Asp.net_SessionID cookie automatically?

这会破坏大多数在具有另一个域的网页中使用的 IFrame 的功能,因为浏览器不会将 ASP.Net 会话 ID 与后续请求一起发送回服务器。

现在,虽然上述线程中有一些建议,但它们对我并不真正有用。这是由于 Safari 的非标准行为。 MacOSX 和 iOS 12.x 上的 Safari 将相同站点属性的值“None”视为未知,因此将值设置为“Strict”,这再次破坏了 Safari 用户的 IFrame 功能。

现在我想知道是否可以在 IIS web.config 中定义出站重写规则,该规则首先检查请求标头以查看客户端是否使用 Safari 浏览器。根据客户端浏览器,版本不同的重写出站规则应更改与浏览器期望相对应的 cookie。

是否可以根据请求编写带有条件的出站规则?我没有找到任何表明此作品的文档或网站...

【问题讨论】:

  • 您可以尝试使用 {HTTP_USER_AGENT} 检查浏览器。您可以尝试使用匹配浏览器的规则并根据该规则设置cookie值:<outboundRules><rule name="cookie rule 1" patternSyntax="Wildcard" stopProcessing="true"><match serverVariable="RESPONSE_Set-Cookie" pattern="*" /><conditions><add input="{HTTP_USER_AGENT}" pattern="*Firefox*" /><add input="{HTTP_USER_AGENT}" pattern="*Edge*" negate="true" /></conditions><action type="Rewrite" value="test" /> </rule> </outboundRules>

标签: iis url-rewriting web-config session-cookies samesite


【解决方案1】:

我修改了几个 SO 答案以提出此 URL 重写,将 SameSite=None 添加到会话 cookie,并从大多数不兼容浏览器的 所有 cookie 中删除 SameSite=None。此次重写的目的是保留 Chrome 80 之前的“遗留”行为。它具体涵盖了您提到的 MacOSX 和 iOS 12.x 上的 Safari 场景。

在我的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)(=.*))(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>

这应该适用于大多数 ASP .Net 和 ASP .Net Core 应用程序,尽管较新的框架具有适当的代码和配置选项来让您控制这种行为。我建议在使用上面的重写之前研究所有可用的选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多