使用 IIS7 的 URL 重写捕获 Cookie 中的值
我需要为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答我自己的问题,这是一个工作示例。该模式可能需要额外的工作,具体取决于需要支持的字符,但以下规则将使用发现的 cookie 值并路由到发现的服务器,并且可以通过 IPv4 地址或名称(字母数字和句点)指定服务器)。
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).
@Geoffrey为了让您的代码支持返回任何cookie值,我推荐这种模式:
作为提醒,{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:
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
以下规则将使用发现的 cookie 值并路由到发现的路径:
该模式支持多个 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:
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
andbat=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 offoo
inpattern="..."
;{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)
.