Deny all files in a directory, via web.config setting
作为测试,我尝试使用 web.config 通过以下方式控制安全性:
所以我将 web.config 设置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?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> <deny users="?"/> <deny users="*"/> </authorization> </system.web> </location> <location path="NonAccessibleDirectory/AccessibleFile.html"> <system.web> </authorization> </system.web> </location> <!-- Allow access to all files in a directory, except for a specific file --> <location path="AccessibleDirectory/NonAccessibleFile.html"> <system.web> <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 发生的事情是它只将允许和拒绝规则应用于托管处理程序处理的文件。
.txt 和 .html 文件等文件由 IIS 处理,而不是 ASP.NET,因此授权规则不适用于它们。
您可以通过将其添加到主 web.config 以使用 IIS 版本来测试这一点。
1 2 3 4 5 6 | <system.webServer> <modules> <remove name="UrlAuthorization" /> </modules> </system.webServer> |
我用相同的安全性和相同的目录和文件对此进行了测试,一切似乎都可以正常工作
如果您使用其他身份验证方法(例如表单),则更完整的版本可能是这个
1 2 3 4 5 6 7 8 9 | <system.webServer> <modules> <remove name="UrlAuthorization" /> <remove name="DefaultAuthentication" /> </modules> </system.webServer> |