在 web.config 中使用 ExecuteURL 作为 404 处理程序将绕过 URL 重写(即..outboundRules),而使用其他 responseModes 不会 [英] Using ExecuteURL as 404 handler in web.config will bypass URL Rewrite (ie.. outboundRules) while using other responseModes won't
问题描述
我在 web.config 中有以下规则,旨在识别和重写具有 secure 和 httpOnly 标志的出站会话 cookie:
I have the following rule in web.config designed to identify and rewrite outbound session cookies with both the secure and httpOnly flags:
<rewrite>
<outboundRules>
<preConditions>
<preCondition name="MatchSessionCookies">
<add input="{RESPONSE_SET_COOKIE}" pattern="." />
</preCondition>
</preConditions>
<rule preCondition="MatchSessionCookies" name="SecureSessionCookies" enabled="true">
<match serverVariable="RESPONSE_SET_COOKIE" pattern="^(.*sess.*)=(.+)$" />
<action type="Rewrite" value="{R:1}={R:2}; httpOnly; secure" />
</rule>
</outboundRules>
</rewrite>
这按预期工作,直到 httpErrors 发挥作用:
This works as intended, up until httpErrors comes into play:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/path/to/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
因此,当访问 /a-page-that-exists.aspx
时,写出的出站 ASPSESSIONID cookie 被 secure 和 httpOnly 成功重写 标志.
So when accessing /a-page-that-exists.aspx
, the outbound ASPSESSIONID cookies that get written out are successfully rewritten with both secure and httpOnly flags.
Request URL: /a-page-that-exists.aspx
Status Code: 200 OK
Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/; httpOnly; secure
问题在于访问 /a-page-that-does-NOT-exist.aspx
.似乎 [404] 请求在内部路由"到 ExecuteURL
路径,并且我的 URL 重写规则完全被绕过.
The problem is accessing /a-page-that-does-NOT-exist.aspx
. It appears that the [404] request is internally "routed" to the ExecuteURL
path and my URL rewrite rules I have in place are bypassed altogether.
Request URL: /a-page-that-does-NOT-exist.aspx
Status Code: 200 OK
Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/
关于如何修改我的出站重写规则以便在将它们传递给我的 404 处理程序之前将它们应用于 [404] 请求的任何想法?
Any ideas on how to modify my outbound rewrite rules so that they can be applied to [404] requests before being handed of to my 404 handler?
推荐答案
好吧,看起来我们必须凑合使用 IIS <httpErrors/>
处理程序的 URL Rewrite 版本,但是它有效:
Well, it looks like we have to make do with a URL Rewrite version of IIS <httpErrors />
handler, but it works:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- Remove existing 404 handler -->
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
</httpErrors>
<rewrite>
<outboundRules>
<preConditions>
<preCondition name="MatchSessionCookies">
<add input="{RESPONSE_SET_COOKIE}" pattern="." />
</preCondition>
</preConditions>
<!-- Does NOT work with ExecuteURL 404 handler -->
<rule preCondition="MatchSessionCookies" name="SecureSessionCookies" enabled="true">
<match serverVariable="RESPONSE_SET_COOKIE" pattern="^(gsm|.*sess.*)=(.+)$" />
<action type="Rewrite" value="{R:1}={R:2}; httpOnly; secure" />
</rule>
</outboundRules>
<rules>
<!-- Re-implement ExecuteURL 404 handler as URL Rewrite -->
<rule name="Handle404" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/path/to/404.aspx?404;{PreserveSchema:{HTTPS}}{HTTP_HOST}{UNENCODED_URL}" />
</rule>
</rules>
<rewriteMaps>
<!-- http://stackoverflow.com/a/10227936/901156 -->
<rewriteMap name="PreserveSchema" defaultValue="OFF">
<add key="ON" value="https://" />
<add key="OFF" value="http://" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
和回应:
Request URL: /a-page-that-does-NOT-exist.aspx
Status Code: 200 OK
Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/; httpOnly; secure
这篇关于在 web.config 中使用 ExecuteURL 作为 404 处理程序将绕过 URL 重写(即..outboundRules),而使用其他 responseModes 不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!