使用 IIS7 的 URL 重写捕获 Cookie 中的值

发布于 2024-12-25 11:30:17 字数 413 浏览 2 评论 0原文

我需要为 IIS 7.5 网站编写一个 URL 重写规则,捕获特定 cookie 中的值,然后使用该值构建 URL。例如,传入的请求如下所示:

GET http://myserver.com/test.aspx HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0
Host: myserver.com
Cookie: foo=bar; bat=bar

我想将它们路由到此(基于“foo”cookie值):

http://myserver.com/bar/test.aspx

在查看文档并搜索示例后,我被难住了!感谢您的帮助。

I need to write a URL Rewrite rule for my IIS 7.5 website that captures a value in a particular cookie, and then uses that value to construct a URL. For instance, the incoming requests looks like this:

GET http://myserver.com/test.aspx HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0
Host: myserver.com
Cookie: foo=bar; bat=bar

I'd like to route them to this (based on the "foo" cookie value):

http://myserver.com/bar/test.aspx

fter reviewing the documentation and searching for examples, I'm stumped! Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能否归途做我良人 2025-01-01 11:30:17

回答我自己的问题,这是一个工作示例。该模式可能需要额外的工作,具体取决于需要支持的字符,但以下规则将使用发现的 cookie 值并路由到发现的服务器,并且可以通过 IPv4 地址或名称(字母数字和句点)指定服务器)。

<rule name="Route Base On Cookie" stopProcessing="true">
  <match url="^(.*)" />
    <conditions>
       <add input="{HTTP_COOKIE}" pattern="foo=(.*?);" />
    </conditions>
  <action type="Rewrite" url="http://{C:1}/{R:0}" />
</rule>

Answering my own question, here's a working example. The pattern may need additional work depending on what characters require supporting, but the following rule will will use the discovered cookie value and route to the discovered server--and the server can be specified by IPv4 address or by name (alphanumeric-and-period).

<rule name="Route Base On Cookie" stopProcessing="true">
  <match url="^(.*)" />
    <conditions>
       <add input="{HTTP_COOKIE}" pattern="foo=(.*?);" />
    </conditions>
  <action type="Rewrite" url="http://{C:1}/{R:0}" />
</rule>
墨落画卷 2025-01-01 11:30:17

@Geoffrey为了让您的代码支持返回任何cookie值,我推荐这种模式:

<add input="{HTTP_COOKIE}" pattern="foo=(.*?);" />

作为提醒,{HTTP_COOKIE}值看起来像这样,例如:

Cookie: foo=myexamplevalue;过期=2014 年 5 月 3 日星期三 22:31:08 GMT;路径=/;仅 Http\r\n

@Geoffrey To make your code support returning any cookie value, I'd recommend this pattern:

<add input="{HTTP_COOKIE}" pattern="foo=(.*?);" />

As a reminder, the {HTTP_COOKIE} value looks like this for example:

Cookie: foo=myexamplevalue; expires=Wed, 03-May-2014 22:31:08 GMT; path=/; HttpOnly\r\n

荒芜了季节 2025-01-01 11:30:17

以下规则将使用发现的 cookie 值并路由到发现的路径:

<rule name="Route Base On Cookie" stopProcessing="true">
  <match url=".*" />
  <conditions>
     <add input="{HTTP_COOKIE}" pattern="^(.+; )?foo=([^;]*)(;.+)?$" />
  </conditions>
  <action type="Rewrite" url="https://{HTTP_HOST}/{C:2}/{R:0}" />
</rule>

该模式支持多个 cookie,并且不依赖于 cookie 中 foo 的顺序(当它是最后一个时,浏览器不依赖) t 附加 ';'):
两个 foo=bar; bat=bar 和 bat=bar; foo=bar 工作正常。
当您有另一个具有相同子字符串的 cookie(例如 barfoo)时,它可以正常工作。
{C:2} 对应 cookie 中的第二个捕获,即 pattern="..."foo 的值;
{R:0} 对应于 中的整个 url。 IIS 仅使用不带主机名的路径部分,即/test.aspx

注意。按原样使用路径中 cookie 的值可能不安全。也许您应该与预期值列表(bar|bar2|bar3|bar4)进行匹配。

The following rule will use the discovered cookie value and route to the discovered path:

<rule name="Route Base On Cookie" stopProcessing="true">
  <match url=".*" />
  <conditions>
     <add input="{HTTP_COOKIE}" pattern="^(.+; )?foo=([^;]*)(;.+)?
quot; />
  </conditions>
  <action type="Rewrite" url="https://{HTTP_HOST}/{C:2}/{R:0}" />
</rule>

The pattern supports several cookies and doesn't depend on the order of foo in cookie (when it is the last, browser doesn't append ';'):
both foo=bar; bat=bar and bat=bar; foo=bar work fine.
It works correctly when you have another cookie with the same substring such as barfoo.
{C:2} corresponds to the second capture in cookie, i.e. value of foo in pattern="...";
{R:0} corresponds to whole url in <match url="...">. IIS uses path part only without host name, i.e. /test.aspx.

NB. It may be not secure to use value from cookie in path as is. Possibly you should match against the list of expected values (bar|bar2|bar3|bar4).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文