简体   繁体   English

如何在Web.Config中禁用目录浏览

[英]How to disable Directory Browse in Web.Config

I have a WebSite and I woud like Deny Directory Browser from a web.config file . 我有一个WebSite,我喜欢来自web.config文件的 拒绝目录浏览器

Here the code I'm using placed in the ROOT. 这里我使用的代码放在ROOT中。 Does not work. 不行。 I'm testing it Locally so with URL LIKE http://localhost:3214/ I can still browser the directory fro CMS. 我正在本地测试它所以使用URL LIKE http:// localhost:3214 /我仍然可以浏览CMS的目录。

  • What I'm doing wrong? 我做错了什么?
  • Should it work even on Local Machine? 它应该在本地机器上工作吗?

Thanks for your support! 谢谢你的支持!

<configuration>
  <location path="Cms">
    <system.webServer>
      <directoryBrowse enabled="false" />
    </system.webServer>
  </location>
</configuration>

Some resource: 一些资源:

http://blogs.iis.net/bills/archive/2008/03/24/how-to-enable-directory-browsing-with-iis7-web-config.aspx http://blogs.iis.net/bills/archive/2008/03/24/how-to-enable-directory-browsing-with-iis7-web-config.aspx

http://www.expta.com/2008/03/configuring-virtual-directories-with.html http://www.expta.com/2008/03/configuring-virtual-directories-with.html

First: not everything works in a <location> block. 首先:并非所有内容都在<location>块中运行。

I cannot find a positive statement that <system.webServer> isn't supported in a <location> , but I cannot find any cases where it is suggested that it is allowed. 我找不到在<location>不支持<system.webServer>的肯定声明,但我找不到任何建议允许它的情况。 Evidence for this is in the (global) machine.config where the system.webserver section is defined to use the IgnoreSection handler: ie. 这方面的证据是在(global) machine.config ,其中system.webserver部分被定义为使用IgnoreSection处理程序:ie。 it isn't processed by the standard System.Configuration implementation, and it is that standard implementation of config handling that provides support for <location> . 它不是由标准的System.Configuration实现处理的,而是为<location>提供支持的配置处理的标准实现。

Try adding a web.config into the Cms folder with the directoryBrowse element to confirm that browsing is blocked. 尝试使用directoryBrowse元素将web.config添加到Cms文件夹中,以确认浏览已被阻止。

Gibbok, nothing is wrong here, Don't panic :) Gibbok,这里没什么不对,不要惊慌:)

Assuming that you are browsing your website from the built-in server inside Visual Studio, This is typically a normal case. 假设您正在从Visual Studio内置的服务器浏览您的网站,这通常是正常情况。

I'm pretty sure that if you moved to IIS with the above code, Directory browsing will behave just like what you want it to be. 我很确定如果你使用上面的代码移动到IIS,目录浏览的行为就像你想要的那样。

Give it a try and host this website in IIS, and let us know what happened, Thanks. 尝试在IIS中托管这个网站,让我们知道发生了什么,谢谢。

the <directoryBrowse> directive controls the listing of the directory via the http protocol only: http://msdn.microsoft.com/en-us/library/ms691327%28v=vs.90%29.aspx <directoryBrowse>指令仅通过http协议控制目录列表:http: //msdn.microsoft.com/en-us/library/ms691327%28v=vs.90%29.aspx

It will not have any effect on code, trying to access the directory from within any other (or even your own) application. 它不会对代码产生任何影响,试图从任何其他(甚至您自己的)应用程序中访问该目录。

@Edit: after the edit and if I get you right - I think, what you mean by Deny Directory Browser is the intended functionality: show the contents of the dir within a specified path using the browser. @Edit:在编辑之后如果我说对了 - 我想, Deny Directory Browser的意思是预期的功能:使用浏览器在指定路径中显示目录的内容。 And yes, it probably will not work in the local development server. 是的,它可能无法在本地开发服务器上运行。 Try it on IIs! 在IIs上尝试吧!

I was trying the same code. 我正在尝试相同的代码。 What you have is in fact correct. 你所拥有的其实是正确的。 Here is the reference . 这是参考

Of course it wasn't working for me and I couldn't figure out why, until I read Richard's post and remembered that someone put a web.config file in that directory. 当然它不适合我,我无法弄清楚为什么,直到我阅读理查德的帖子并记得有人在该目录中放置了一个web.config文件。 That file overrides the behavior in the main web.config file. 该文件将覆盖主web.config文件中的行为。 In the sub directory web.config the directoryBrowse option was explicitly turned on (as well as a LOT of other handlers, yikes!). 在子目录web.config中,显式启用了directoryBrowse选项(以及许多其他处理程序,yikes!)。 Delete that file and what you have above works for me. 删除该文件以及上面的内容对我有用。 It actually passes the request along to the next handler, which is exactly what I wanted. 它实际上将请求传递给下一个处理程序,这正是我想要的。

I'm using IIS 7 on my local machine. 我在本地计算机上使用IIS 7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM