Jer*_*emy 7 asp.net security web-config
作为测试,我尝试使用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>
Run Code Online (Sandbox Code Playgroud)
正如所料:
我遇到的问题是:
艾米我配错了吗?
您可能正在运行ASP.NET URL授权和IIS URL授权之间的区别.有关这方面的详细摘要,请访问http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences
简而言之,默认情况下,使用web.config在ASP.NET中发生的情况是,它仅将允许和拒绝规则应用于托管处理程序处理的文件.
.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>
Run Code Online (Sandbox Code Playgroud)
我使用相同的安全性和相同的目录和文件对此进行了测试,所有这些都可以正常工作
如果您使用其他身份验证方法(如表单),则可以使用更完整的版本
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3251 次 |
| 最近记录: |