IIS 7.5:如何使用 Windows 身份验证配置自定义身份验证错误页面。401标题问题

tra*_*max 15 iis-7.5 windows-authentication custom-errors

我有一个在 IIS 7.5 下运行的 php 网站。该站点受 Windows 身份验证保护,并且运行良好:

Windows 身份验证已开启

当用户访问该站点时,他们会被要求输入用户名/密码,并在通过身份验证后通过。如果用户单击取消或错误输入密码 3 次,则会显示 401 错误页面:

丑陋的401页面

现在我想展示解释如何登录的自定义页面。所以我转到错误页面,选择状态代码 401.2 并将其指向我想要显示的页面:

错误页面设置

然后确保为每个人打开自定义错误。和 kaa-boom!身份验证不再起作用,用户不会看到密码提示。正如文档所说,Windows 身份验证的工作原理是首先发送 401 回复,然后浏览器要求用户提供凭据,然后他们确定下一步要做什么。

这里发生了什么:在第一次请求页面时,IIS 尝试发送 401 标头,但注意到 web.config 显示“在 401 上重定向到此页面”。而不是身份验证,它只提供重定向页面。

我试过更换 401、401.1、401.2 - 没有区别。

我做错了什么以及如何在用户身份验证错误时提供自定义页面?

ps 这是 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="Custom">
            <remove statusCode="500" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <remove statusCode="401" subStatusCode="-1" />
            <error statusCode="401" subStatusCode="2" prefixLanguageFilePath="" path="/not_restricted/401.htm" responseMode="ExecuteURL" />
            <error statusCode="404" prefixLanguageFilePath="" path="/not_restricted/404.htm" responseMode="ExecuteURL" />
        </httpErrors>
        <httpProtocol>
            <customHeaders>
                <remove name="X-Powered-By" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
    <system.web>
        <identity impersonate="false" />
        <customErrors defaultRedirect="http://www.myserver.com/not_restricted/500.htm" mode="Off">
        </customErrors>
    </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Pet*_*orf 15

尝试这个:

改变:

<error statusCode="401" subStatusCode="2" prefixLanguageFilePath="" path="/not_restricted/401.htm" responseMode="ExecuteURL" />
Run Code Online (Sandbox Code Playgroud)

<error statusCode="401" subStatusCode="2" prefixLanguageFilePath="" path="not_restricted\401.htm" responseMode="File" />
Run Code Online (Sandbox Code Playgroud)

在响应模式“文件”的情况下,IIS 只是加载该文件的内容并显示它,它仍然将 401 状态发送回客户端。

我曾经使用“ExecuteURL”,但了解到文件模式效果更好。您只需要确保错误页面中的任何链接资源仍然有效。