拒绝目录中的所有文件,通过web.config中设置 [英] Deny all files in a directory, via web.config setting

查看:185
本文介绍了拒绝目录中的所有文件,通过web.config中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个测试,我试图用web.config中控制通过以下方式安全性:

As a test, I'm trying to use the web.config to control security in the following ways:


  1. 拒绝访问的所有目录中的文件,除了特定的文件

  2. 允许访问的所有目录中的文件,除了特定的文件

因此​​,我成立了web.config中如下:

So I set up the web.config as follows:

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

正如预期的那样:

As expected:


  • 如果我浏览到非访问目录中,并没有指定一个文件,我得到拒绝访问

  • 如果我浏览到访问目录中,并没有指定一个文件,我可以看到文件列表

时遇到的问题是:


  • 如果我浏览到非访问目录中,并指定一个文件,我可以看到它,我本来期望不被授予访问权限

  • 如果我浏览到访问目录中,并指定我已经通过在web.config拒绝访问的文件,我仍然可以查看它,我本来期望不被授予访问权限

艾米我配置的东西错了吗?

Amy I configuring things wrong?

推荐答案

您可以在到之间的差异正在运行的 ASP.NET URL授权 IIS URL授权。基于此的一个详细的总结是<一个href=\"http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences\" rel=\"nofollow\">http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences

You may be running in to the difference between ASP.NET URL Authorization and IIS URL Authorization. A detailed summary on this is at http://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization#Differences

简言之,什么在默认情况下的web.config与ASP.NET的情况是,它仅适用的允许拒绝的规则来被管理处理程序处理的文件。

Briefly, what happens with ASP.NET by default with web.config is that it only apply the allow and deny rules to files handled by the managed handler.

文件如.txt和.html文件由IIS处理,而不是ASP.NET,因此授权规则并不适用于他们。

Files such as .txt and .html files are handled by IIS and not ASP.NET, so the authorization rules aren't applied to them.

您可以通过添加这对你的主要的web.config使用IIS版本测试了这一点。

You can test this out by adding this to your main web.config to use the IIS version.

<system.webServer>
    <modules>
        <remove name="UrlAuthorization" />
        <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"  />
    </modules>
</system.webServer>

我与你相同的安全性和相同的目录和文件进行了测试,一切似乎工作

I tested this with your same security and same directories and files, and all appears to work

一个更完整的版本,如果你使用其他身份验证方法,如形式可能是这个

A more complete version if you use other authentication methods such as forms could be this

<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中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆