从 windows 身份验证中排除路径

Exclude Path from windows authentication

我在 Windows Server 2016 上有一个 IIS。 它用作我们的内部网,并且启用了 windows- 身份验证以使用用户登录。 这一切都很完美。

现在我们想开发一个 api 使用相同的服务器。因此,我需要从 windows 身份验证中排除一条路径,并使其可用于匿名连接。 路径例如“[server]/api/”将由 PHP 处理,因此没有 'physical' /api 文件夹。

我用在互联网上找到的以下部分编辑了 web.config

<location path="Default Web Site/api">
    <system.web>
     <authorization>
        <allow users="?"/>
     </authorization>
  </system.web>
</location>

我的第二次尝试是改变

<section name="anonymousAuthentication" overrideModeDefault="Deny" />

<section name="anonymousAuthentication" overrideModeDefault="Allow" />

在 applicationHost.config 中并将以下内容添加到 web.config

<location path="Path/To/Public/Folder">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
            </authentication>
        </security>
    </system.webServer>
</location>

这两种尝试都不起作用,如果我打开 [server]/api 它仍然要求我提供凭据..感谢任何帮助。


更新: 我按照 MisterSmith 给定的 link 并将 applicationHost.config Deny 编辑为 Allow

<section name="access" overrideModeDefault="Allow" />
<section name="anonymousAuthentication" overrideModeDefault="Allow" />
<section name="windowsAuthentication" overrideModeDefault="Allow" /> 

和added/replaced我的web.config

中的以下内容
<location path="api">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
    <system.webServer> 
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
            </authentication>
        </security>
    </system.webServer>
</location>

但我仍然收到 /api 的身份验证请求我认为这是一个简单的错误,但我无法弄清楚我错过了什么.. 我有一个 64 位 OS 并使用了一个 64 位记事本 ++,但为了确保我尝试了推荐的记事本 2 和 notepad.exe 中的构建,但没有成功。 为了确保我对 web.config 的编辑没有导致错误,这里总共是

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpProtocol>
         <customHeaders>
           <add name="Access-Control-Allow-Origin" value="<server>" />
           <add name="Access-Control-Allow-Credentials" value="true" />        
         </customHeaders>
       </httpProtocol>
        <rewrite>
            <rules>
                <rule name="Importierte Regel 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
        <httpErrors errorMode="Detailed" />
        <security>
            <authentication>
                <anonymousAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
    <location path="api">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
        <system.webServer> 
            <security>
                <authentication>
                    <anonymousAuthentication enabled="true" />
                </authentication>
            </security>
        </system.webServer>
    </location>

您的关闭,但您的位置块和 applicationhosts 缺少一些位 - 参见 post 。此外,如果你有奇怪的行为,请注意有关文本编辑器的 32/64 位“位数”的评论 - 如果你使用 32 位编辑器 Windows 将为你提供 32 位 applicationHost 文件,而 64 位编辑器 windows returns 64 位 applicationHost 文件(如果有疑问,常规内置 notepad.exe 始终是 64 位 Windows 上的 64 位应用程序)。

在 MisterSmith 的帮助下找到的 'error' 是我找错了位置。因为我的路由通常在物理上不存在,而只在 php 脚本中处理,所以我在 web.config 中有重写规则 这导致 <server>/test 被重写为 <server>/index.php/test

考虑到这一点,需要将位置块中的路径从 <location path="api"> 更改为 <location path="index.php/api">,这样问题就解决了!