【问题标题】:Deny all files in a directory, via web.config setting通过 web.config 设置拒绝目录中的所有文件
【发布时间】:2016-05-06 17:39:06
【问题描述】:

作为测试,我正在尝试使用 web.config 通过以下方式控制安全性:

  1. 拒绝访问目录中的所有文件,特定文件除外
  2. 允许访问目录中的所有文件,特定文件除外

所以我将 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 security web-config


    【解决方案1】:

    您可能会遇到 ASP.NET URL 授权IIS URL 授权 之间的区别。对此的详细总结在http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences

    简而言之,ASP.NET 默认情况下使用 web.config 发生的情况是,它仅将 allowdeny 规则应用于托管处理程序处理的文件。

    .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>
    

    【讨论】:

    • 这正是正在发生的事情,这是有道理的,因为在我的页面中,我使用 System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal() 方法来禁用用户无权访问的链接,即使我可以通过输入 url 访问文件,它们也会正确禁用。
    • 将我的 html 页面更改为 aspx 页面解决了我的问题。与保留 html 页面一样,但使用 System.Web.Security.UrlAuthorizationModule 进行 UrlAuthorization
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2011-11-30
    • 2015-07-30
    • 2016-03-08
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多