使用Web.config阻止对子目录的访问


8

我的ASP.NET项目中有一个包含实用程序文件的子目录。运行时代码需要它们,但是我不希望它们在网络上可见。

Web.config文件中用来阻止所有用户访问单个子目录及其所有内容的语法是什么?

Answers:


15

IIS 7具有新的“请求过滤”功能。您可能要使用隐藏的段配置:

<configuration>
 <system.webServer>
  <security>
   <requestFiltering>
    <hiddenSegments>
     <add segment="BIN"/>
    </hiddenSegments>
   </requestFiltering>
  </security>
 </system.webServer>
</configuration>

这将导致http:// yoursite / bin无法使用(但http:// yoursite / binary仍然有效)

检出:http : //learn.iis.net/page.aspx/143/how-to-use-request-filtering


2
但这也会阻止yoursite / bla / bin / test .... :-(
CarstenSchütte13年

这不是问题的答案,但对当前的主题很有用。您也可以使用HttpHandlers阻止所需的特定文件扩展名。这是很好的做法做的一样好,例如阻止任何源/数据库文件等

1

您的问题是,如果IIS仅返回文件ASP.Net,将永远不会有任何干扰的机会。我相信可以通过启用基于表单的身份验证和相当多的操作来做到这一点,但是我只是将文件移到wwwroot文件夹之外。

JR


0

这应该工作:

<configuration>
  <location path="FolderName">
    <system.web>
      <authorization>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
</configuration>

2
那是行不通的,因为文件是.txt文件...我认为John Rennie是对的,因为ASP.NET没有机会干扰.txt文件。
乔尔·斯波斯基

很高兴知道,我一直将通过ASP.NET的处理程序提供的文件专门保留在可通过Web访问的目录中,所以我从来没有遇到这个问题
John Rasch
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.