IIS重写规则,用于查询字符串上的基本身份验证

时间:2012-02-28 20:07:43

标签: asp.net iis url-rewriting iis-7.5 xwiki

我正在尝试通过基本身份验证自动将用户登录到Xwiki安装。这是因为帮助存储在wiki中,但我们希望检索过程对用户透明。

我们将用户推送到网址(通过<a>标记),例如: http://username:password@xwiki.example.org/xwiki/bin/view/Main?basicauth=1

除了Internet Explorer之外,每个浏览器都可以正常工作(请参阅:http://support.microsoft.com/kb/834489。不幸的是,80%的用户群使用Internet Explorer,并且不能让他们手动输入凭据。

目前,我们将IIS 7.5放在Xwiki前面,并将所有请求代理到另一台服务器上的Tomcat实例。这很好用。为了解决我的问题,我想我可以使用IIS重写规则来转换这样的URL:

http://xwiki.example.org/xwiki/bin/view/Main?basicauth=1&_username=username&_password=password

进入这个:

http://username:password@xwiki.example.org/xwiki/bin/view/Main?basicauth=1&_username=username&_password=password

这个想法是IIS会将_username / _password查询字符串参数替换为URL并将其传递给Tomcat,而Xwiki会忽略额外的参数。

我创建了一个URL重写规则,如:

<rule name="BasicAuthRewrite" enabled="true">
   <match url="https?://(.+)&amp;?_username=(.+)&amp;_password=(.+)" />
   <action type="Rewrite" url="http://{R:2}:{R:3}@xwiki.example.org/{R:1}" />
</rule>

当我在IIS中使用'测试模式'并提供我的网址时,所有后向引用({R:x})都匹配到我想要的数据。但是,当我在浏览器中访问URL时,重写规则无法调用。

有什么方法可以实现我想要的行为吗?

4 个答案:

答案 0 :(得分:9)

可以在IIS上使用URL重写进行基本身份验证。您应该在base64中添加服务器变量HTTP_Authorization,其值为Basic,后跟username:password。请记住在允许的变量中添加变量

因此,对于密码打开芝麻的用户Aladdin,您的格式为Aladdin:open sesame和base64 encoded QWxhZGRpbjpvcGVuIHNlc2FtZQ ==。

转化为 授权:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==

<rule name="SomeName" stopProcessing="true">
    <match url="url/to/match" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Rewrite" url="http://www.redirecturl.com/" appendQueryString="true" />
    <serverVariables>
        <set name="HTTP_Authorization" value="Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" />
    </serverVariables>
</rule>

IIS Screenshot Authentication

答案 1 :(得分:0)

这应该做:

<rule name="BasicAuthRewrite" stopProcessing="true">
    <match url="(.*)" />
    <conditions trackAllCaptures="true">
        <add input="{QUERY_STRING}" pattern="basicauth=1&amp;_username=(.+)&amp;_password=(.+)" />
    </conditions>
    <action type="Rewrite" url="http://{C:1}:{C:2}@xwiki.example.org/{R:1}" appendQueryString="false" />
</rule>

答案 2 :(得分:0)

授权不能委托给ARR。因此,如果内容本质上是高度敏感的并且需要授权,则建议您不要启用缓存。 ARR

但是有解决方案。

Solution

答案 3 :(得分:-1)

看来这在IIS中是不可能的。