如何使 IIS 根据 Windows 用户名或组成员身份授权请求?

How to make IIS authorize requests based on Windows user name or group membership?

我有一个由 IIS 使用 PHP 托管的遗留 Web 应用程序。使用根目录 web.config 中的以下配置限制对该应用程序的某些目录的访问。这使得 Windows 用户名可用作 REMOTE_USER,以便应用程序可以将该用户名映射到单独的数据库以检查授权。这行得通,不得更改。

<location path="lsgprog/bibliothek/adm">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication    enabled="false" />
                <windowsAuthentication      enabled="true"  />
            </authentication>
        </security>
    </system.webServer>
</location>

访问其他一些目录以及使用 Windows 提供的凭据也受到限制。因此,那些其他目录已禁用 anonymousAuthentication 并也启用了 windowsAuthentication。不同之处在于 1. 这些设置是在 IIS 的 GUI 中进行的,以及 2. 该授权实际上是针对文件系统进行检查的。这意味着目录仅对某些特殊用户组具有读取权限,这些组和用户由某些 Active Directory 维护,并且由于该应用程序使用 Windows 身份验证,因此一切正常。用户在他们的 Windows 进行身份验证,打开 Internet Explorer,请求站点的受限部分,IIS 获取用户名、组成员身份等,检查对文件系统中受限目录的访问并授予或拒绝它。

所有这些都是使用 IIS 的 GUI 手动配置的,我想将其迁移到 web.config。上面已经记录了为某些目录启用 Windows 身份验证,我缺少的是如何 allow/deny 访问用户和组,这是文件系统部分。我已经找到了元素 authorization,它看起来很像我想要的,但无论我尝试什么都不行。

<location path="lsgprog/vfristen">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication    enabled="false" />
                <windowsAuthentication      enabled="true"  />
            </authentication>
        </security>
    </system.webServer>

    <system.web>
        <authorization>
            <deny   users="*"
                    roles="*"
                    verbs="GET,HEAD,POST" />
        </authorization>
    </system.web>
</location>

我的期望是以上足以拒绝所有用户的访问,但这不起作用,任何基于 ALLOW 的方法也不行。我希望 usersroles 可以简单地映射到当前请求用户的用户名和组名。我不想要的是基于表单的授权或将目录转换为“应用程序”或任何需要在 web.config.

之外完成的事情

那么,我尝试做的事情是否可行?如果可行,怎么做?谢谢!

您可以尝试在您的站点 web.config 文件中添加以下代码:

    <location path="foldername">
        <system.webServer>
            <security>
                <authentication>
                    <anonymousAuthentication enabled="false" />
                    <windowsAuthentication enabled="true" />
                </authentication>
            </security>
        </system.webServer>
    </location>
<location path="foldername/page1.php">
        <system.webServer>
            <security>
                <authorization>
                    <remove users="*" roles="" verbs="" />
                    <add accessType="Allow" roles="DOMAIN\ADGROUP" />
                    <add accessType="Deny" users="*" />
                </authorization>
            </security>
        </system.webServer>
    </location>

编辑:需要在 iis 中安装 URL 授权才能使此规则生效。

https://docs.microsoft.com/en-us/iis/manage/configuring-security/understanding-iis-url-authorization

在这种情况下有多种选择,首先 - 尝试将 web.config 文件添加到需要拥有自己权限的文件夹,例如在 lsgprog/vfristen 下,将拒绝所有用户访问的最小 web.config 示例:

<?xml version="1.0"?>
<configuration>
    <system.web>
      <authorization>
        <deny users="*" />
      </authorization>
    </system.web>
</configuration>

它为什么起作用 - IIS 查看 web.config 个文件的每个文件夹结构,在这种情况下,子项将覆盖父项 但仅覆盖子项中的节点 - 这意味着它将保留父级(根)web.config 的所有其他设置: Make application and directory-specific configuration settings in an ASP.NET application

虽然上面的文档是针对 ASP.NET 的,但它也适用于 IIS 级别。

尝试第二个选项 - 因为问题提到项目的根目录是 lsgprog 那么 web.config 中的这个设置:

<location path="lsgprog/vfristen">

应改为(从路径中删除项目的根文件夹):

<location path="vfristen">

最后,第三个选项也可以工作,如上述文档中所述,在 Machine.config 级别覆盖它: Use the location element in the Machine.config file

When the allowOverride attribute is false, the Web.config files in the web application directories can't override the settings that you specified in the element. This is a useful setting in environments where you must restrict application developers in how they configure a web application. The following example shows a part of a Machine.config file that requires authentication to access the MyApp application on the default Web site and that can't be overridden by settings in a Web.config file:

Machine.config

<configuration>
    <location path="Default Web Site/MyApp" allowOverride="false">
        <system.web>
            <authorization>
                <allow users="?" />
            </authorization>
        </system.web>
    </location>
</configuration>