如何在 Azure 上限制对网站 运行 部分的访问

How to restrict access to section of website running on Azure

我在 Azure 上有一个 Episerver 网站 运行,出于安全原因,我想使用 IP 地址白名单阻止对 cms 管理部分的任何请求的访问。

我过去曾在 windows 服务器上的网站 运行 上这样做过,但我从未在 Azure 托管站点上这样做过。我已经尝试过我在以前的网站上采用的方法,将安全部分添加到 web.config 以用于我试图限制的位置,例如:

<location path="cms/admin">
 <system.webServer>
  </ipSecurity>
   <add allowed="true" ipAddress="{my ip address}" subnetMask="255.255.255.255" />
...
  </security>
 </system.webServer>
</location>

这在本地有效,但当我将 web.config 部署到 Azure 时它不起作用。它会阻止任何用户(包括白名单中的用户)访问该位置。

我也考虑过使用 application->networking->Access-restrictions 在 portal.azure 中进行更改,但这看起来是为了控制对整个应用程序的访问,这不是我想要的.

有谁知道我是否做错了,特别是针对 Azure 网站?是否有我错过的访问限制设置?

谢谢

山姆

您可以使用iis url 重写规则来阻止请求限制特定路径的ip:

<rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="cms/admin" />
                    <conditions>
                        <add input="{REMOTE_ADDR}" pattern="192.168.2.*" />
                    </conditions>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
                </rule>

如果你想允许一些 ip,你可以添加另一个不匹配模式的条件。

更详细的可以参考下面的文章:

Creating Rewrite Rules

Request Blocking - rule

我们使用 url 重写模块解决了这个问题,正如其他人所建议的那样。我们还意识到,由于 cloudflare CDN,传入的 IP 地址不是真正的请求源 IP 地址。幸运的是,原始 IP 地址包含在 Cloudflare 的重新路由请求中,因此我们能够使用下面的 url 重写规则来完成这项工作。我已将此添加为问题的正确答案,因为 Jalpa 的答案在技术上不适用于我的特定上下文:

<rewrite>
    <rules>
        <rule name="restrict admin access by IP address" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_URI}" pattern="^/admin/login(.*)" />
                <!-- localhost -->
                <add input="{HTTP_True_Client_IP}" pattern="^127\.0\.0\.1$" negate="true"/>
                <!-- my office -->
                <add input="{HTTP_True_Client_IP}" pattern="^{your ip address here}$" negate="true"/>
            </conditions>
            <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." />
        </rule>
    </rules>
</rewrite>

这最近对我有用。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Block unauthorized access to admin" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <!-- Enter your staging site host name here as the pattern-->
                        <add input="{REQUEST_URI}" pattern="^/admin" />
                        <!-- Enter your white listed IP addresses -->
                        <add input="{REMOTE_ADDR}" pattern="127\.0\.0\.1" negate="true" />
                        <add input="{REMOTE_ADDR}" pattern="127\.0\.0\.2" negate="true" />
                        <!-- <add input="{REMOTE_ADDR}" pattern="123\.123\.123\.2" negate="true"/> -->
                    </conditions>
                    <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="Site is not accessible" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>