通过 web.config 设置拒绝目录中的所有文件
Deny all files in a directory, via web.config setting
作为测试,我尝试使用 web.config 通过以下方式控制安全性:
- 拒绝访问目录中的所有文件,特定文件除外
- 允许访问目录中的所有文件,特定文件除外
所以我设置 web.config 如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Deny access to all files in a directory, except for a specific file -->
<location path="NonAccessibleDirectory">
<system.web>
<authorization>
<deny users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="NonAccessibleDirectory/AccessibleFile.html">
<system.web>
<authorization>
<allow users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
<!-- Allow access to all files in a directory, except for a specific file -->
<location path="AccessibleDirectory/NonAccessibleFile.html">
<system.web>
<authorization>
<deny users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
符合预期:
- 如果我浏览到不可访问的目录并且没有指定文件,我会被拒绝访问
- 如果我浏览到可访问的目录并且没有指定文件,我可以看到文件列表
我遇到的问题是:
- 如果我浏览到不可访问的目录并指定一个文件,我可以查看它,我本以为不会被授予访问权限
- 如果我浏览到可访问的目录并指定我已通过 web.config 拒绝访问的文件,我仍然可以查看它,并且我预计不会被授予访问权限
艾米我配置错了吗?
您可能 运行 了解 ASP.NET URL 授权 和 IIS URL 之间的区别]授权。 http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences
中有详细的总结
简而言之,默认情况下 ASP.NET 与 web.config 发生的情况是它仅应用 allow 和 deny 由托管处理程序处理的文件规则。
.txt 和 .html 等文件由 IIS 而不是 ASP.NET 处理,因此授权规则不适用于它们。
您可以通过将此添加到主 web.config 以使用 IIS 版本来进行测试。
<system.webServer>
<modules>
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
</modules>
</system.webServer>
我使用与您相同的安全性以及相同的目录和文件对此进行了测试,一切似乎都有效
如果您使用表单等其他身份验证方法,则更完整的版本可能是这个
<system.webServer>
<modules>
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>
</system.webServer>
作为测试,我尝试使用 web.config 通过以下方式控制安全性:
- 拒绝访问目录中的所有文件,特定文件除外
- 允许访问目录中的所有文件,特定文件除外
所以我设置 web.config 如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Deny access to all files in a directory, except for a specific file -->
<location path="NonAccessibleDirectory">
<system.web>
<authorization>
<deny users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="NonAccessibleDirectory/AccessibleFile.html">
<system.web>
<authorization>
<allow users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
<!-- Allow access to all files in a directory, except for a specific file -->
<location path="AccessibleDirectory/NonAccessibleFile.html">
<system.web>
<authorization>
<deny users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
符合预期:
- 如果我浏览到不可访问的目录并且没有指定文件,我会被拒绝访问
- 如果我浏览到可访问的目录并且没有指定文件,我可以看到文件列表
我遇到的问题是:
- 如果我浏览到不可访问的目录并指定一个文件,我可以查看它,我本以为不会被授予访问权限
- 如果我浏览到可访问的目录并指定我已通过 web.config 拒绝访问的文件,我仍然可以查看它,并且我预计不会被授予访问权限
艾米我配置错了吗?
您可能 运行 了解 ASP.NET URL 授权 和 IIS URL 之间的区别]授权。 http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences
中有详细的总结简而言之,默认情况下 ASP.NET 与 web.config 发生的情况是它仅应用 allow 和 deny 由托管处理程序处理的文件规则。
.txt 和 .html 等文件由 IIS 而不是 ASP.NET 处理,因此授权规则不适用于它们。
您可以通过将此添加到主 web.config 以使用 IIS 版本来进行测试。
<system.webServer>
<modules>
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
</modules>
</system.webServer>
我使用与您相同的安全性以及相同的目录和文件对此进行了测试,一切似乎都有效
如果您使用表单等其他身份验证方法,则更完整的版本可能是这个
<system.webServer>
<modules>
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>
</system.webServer>