首页 > 解决方案 > IIS 重写模块 Windows 身份验证

问题描述

我有两个 asp.net web 应用程序。其中一个应用程序是主 mvc Web 应用程序,第二个是充当反向代理的 Web 应用程序,仅包含一个文件 - web.config。反向代理没有启用任何身份验证模式,但主应用程序具有 Windows 身份验证。通过反向代理访问应用程序时,浏览器中会出现弹出窗口,要求您提供 Windows 凭据。是否可以通过所有反向代理请求以某种方式传递一个域用户?当反向代理重定向请求时,它会添加自定义标头。是否可以从 iis 池传递用户或以某种方式硬编码,以便所有反向代理请求都可以通过 windows auth 传递到主应用程序,然后用户可以通过正常登录页面进行身份验证?目标是通过反向代理访问主应用程序,而无需输入 Windows 凭据。无法在主应用程序上禁用 Windows 身份验证。

反向代理 web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                <match url="(.*)"/>
                <action type="Rewrite" url="https://main-app.com/{R:1}"/>
                <serverVariables>
                    <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}"/>
                    <set name="HTTP_ACCEPT_ENCODING" value=""/>
                    <set name="HTTP_CUSTOM_ZEW_HEADER" value="True"/>
                </serverVariables>
            </rule>
        </rules>
        <outboundRules>
            <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
                <match filterByTags="A, Form, Img" pattern="^http(s)?://main-app.com/(.*)"/>
                <action type="Rewrite" value="http{R:1}://main-app.com/{R:2}"/>
            </rule>
            <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
                <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)"/>
                <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}"/>
            </rule>
            <preConditions>
                <preCondition name="ResponseIsHtml1">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/>
                </preCondition>
                <preCondition name="NeedsRestoringAcceptEncoding">
                    <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+"/>
                </preCondition>
            </preConditions>
        </outboundRules>
    </rewrite>
</system.webServer>

标签: c#asp.netiisnetworkingproxy

解决方案


无法转发REMOTE_USER标头,因为当 Authorization 标头存在时,请求在身份验证模块运行之前被转发,因此未设置身份验证服务器变量(当映射到标头时,它们只是通过空白)。

您可以使用发送经过身份验证的用户自定义标头的自定义 HTTP 模块。

另一种方法是您可以设置 SPN:

https://docs.microsoft.com/en-us/archive/blogs/benjaminperkins/configure-application-request-routing-with-windows-authentication-kerberos

https://docs.microsoft.com/en-us/archive/blogs/asiatech/a-quick-solution-when-windows-authentication-is-required-on-backend-web-server-for-arr-scenario


推荐阅读