IIS 重写规则未设置 HTTP_COOKIE

标签 iis iis-7 url-rewriting

我正在与一个通过第三方设置移动网站的客户合作。目前,我们通过 IIS 检查用户代理是否与您的任何标准移动代理匹配,在这种情况下,我们会将用户重定向到移动版本 m.whatever.com。

我们的规则之一要求我们在用户想要再次查看移动网站时将 cookie 的值设置为 0。

<rules>
    <rule name="if httpcookie is , set it to 0" stopProcessing="true">
        <match url="^(.*)$" />
        <conditions>
            <add input="{HTTP_COOKIE}" pattern="mobileoptout=1" />
        </conditions>
        <serverVariables>
            <set name="HTTP_COOKIE" value="mobileoptout=0" />
        </serverVariables>
        <action type="None" />
    </rule>
</rules>

按照上述,我们匹配 URL 和 cookie 值。我已经独立测试了这些,它们按预期工作。然而,在这条规则的最后,cookie MobileOptOut 的值仍然是 1 而不是 0。

我已经搜索并尝试了许多网站上可用的所有示例,但完全无法理解为什么 cookie 的值没有被更改。

cookie 的域是 [whatever.com],与 www.whatever.com 相同,并且根据之前的测试,它可以从 cookie 中读取以验证条件。

有什么想法吗?

包括一次同样无效的额外尝试:

<rules>
   <rule name="set cookie">
     <match url="(.*)" />
     <serverVariables>
       <set name="HTTP_COOKIE" value="optout=1" />
       <set name="{HTTP_COOKIE}" value="optout=2" />
     </serverVariables>
     <action type="None" />
   </rule>
</rules>

最佳答案

感谢@cheesemacfly,我研究了出站规则,出于某种原因我完全忽略了这一点。

该解决方案使用单个入站规则来检查两个条件。

<rule name="MobileOptOut=1 Stop Processing Rules" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
    <add input="{QUERY_STRING}" pattern="mobileoptout=1" />
    <add input="{HTTP_COOKIE}" pattern="MobileOptOut=1" />
  </conditions>
  <action type="None" />
</rule>

这将处理带有查询字符串的第一个请求,以及所有带有 cookie 的后续请求,这些 cookie 将在该规则完成后创建。

出站规则如下:

<outboundRules>
  <rule name="if querystring=1" preCondition="If mobileoptout query = 1">
    <match serverVariable="RESPONSE_Set_Cookie" pattern="." />
    <action type="Rewrite" value="mobileoptout=1; Domain=site.local; Path=/;" />
  </rule>
  <preConditions>
    <preCondition name="If mobileoptout query = 1">
      <add input="{QUERY_STRING}" pattern="mobileoptout=1" />
    </preCondition>
  </preConditions>
</outboundRules>

这将检查 if querystring 的第一个条件是 mobileoptout=1。如果为 true,它将在名为“mobileoptout”的响应中设置一个 cookie,根域中的值为 1,该 cookie 将随 session 一起过期。

这正是我所忽略的。

关于IIS 重写规则未设置 HTTP_COOKIE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16612836/

相关文章:

c# - 没有页面名称的 url 重写

asp.net - IE超时后重新发送请求?

c# - ServiceController 关闭 IIS 安全访问

visual-studio - VS2008,IIS7 Web项目,非管理员。什么时候?

asp.net - Windows Server 2008 R2 + IIS 7.5 + ASP.NET 4.0 = HTTP 错误 500.0

iis - ISAPI 设置在 IIS7 中不可见

azure - 策略 rewrite-uri 在 Azure APIM 中附加上下文变量

c# - 如何使用 ASP.NET 4.0 URL 重写来重写 URL?

c# - 在 ASP.NET 中是否可以单独从 HTTP 请求的用户代理字符串派生浏览器 MajorVersion?

asp.net - 即使禁用 IIS 压缩,为什么 ScriptResource.axd 仍会被压缩?