首页 > 解决方案 > pattern="." 是什么意思 在 IIS Url Rewrite 的 preCondition 属性上?

问题描述

我见过很多 IIS URL Rewrite 的例子:

<!-- reference: https://im5tu.io/article/2017/06/ensuring-samesite-cookies-with-url-rewrite/ -->
<rewrite>
  <outboundRules> 
    <rule name="Ensure samesite Cookies" preCondition="Missing samesite cookie">
      <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
      <action type="Rewrite" value="{R:0}; SameSite=strict" />
    </rule>
    <preConditions>
      <preCondition name="Missing samesite cookie">
        <!-- Dont remove the first line here, it does do stuff! -->
        <add input="{RESPONSE_Set_Cookie}" pattern="." />
        <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=strict" negate="true" />
      </preCondition>
    </preconditions>
  </outboundRules>
</rewrite>

有谁知道为什么<add input="{RESPONSE_Set_Cookie}" pattern="." />需要这条线?

我尝试在 Google 上搜索,但没有找到答案。

标签: asp.netasp.net-mvciis

解决方案


该规则仅在与前置条件匹配时才执行。

“模式”是一个正则表达式,可以.匹配任何字符。
在这种情况下,基本上是说“如果 cookie 有值则匹配”。

如果 cookie 已经具有要添加的值,则它下面的行否定执行规则。
否则,每次运行规则时,您都会再次添加文本。

至于为什么需要,发送一个空的cookie应该删除它。
在这种情况下,它会在尝试使用规则更改它之前确保 cookie 中确实存在一个值。

重写模块 - 前置条件集合
为 URL 重写模块创建出站规则


推荐阅读