Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use wildcards in the web.config location path attribute?

In IIS 7 I try to deny access to all files with the extension .xml for all users.

I tried the following setting in my web.config file:

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

But then getting any file results in an internal server error.

It works if I deny access to the individual files but this solution does not buy me much as I do not know all .xml files in advance.

like image 749
Alexander Egger Avatar asked Oct 27 '09 14:10

Alexander Egger


People also ask

What is location path in web config?

The <location> element typically contains a <system. web> element and other configuration elements exactly as you use them in the Web. config file. The path attribute of the <location> element specifies the virtual directory or the file name where the location configuration items apply.

How do I add file path to web config?

string filePath = ConfigurationManager. AppSettings["Path"]. ToString();

Where do I put appSettings in web config?

Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.

Where is the IIS Web config file located?

The configuration files for IIS 7 and later are located in your %WinDir%\System32\Inetsrv\Config folder, and the primary configuration files are: ApplicationHost. config - This configuration file stores the settings for all your Web sites and applications. Administration.


2 Answers

Try this:

<configuration>
    <system.web>
        <httpHandlers>
            <add path="*.xml" verb="*" 
             type="System.Web.HttpNotFoundHandler" />
        </httpHandlers>
    </system.web>
</configuration>

By the way you could alternatively store all of your xml files within the App_Data directory. Storing files of any type in this directory will not be served to the web.

like image 100
David Glass Avatar answered Oct 05 '22 22:10

David Glass


Another way is to use a request filter:

<system.webServer>
  <security>
    <requestFiltering>
      <fileExtensions>
        <add fileExtension=".xml" allowed="false" />
      </fileExtensions>
    </requestFiltering>
  </security>
</system.webServer>
like image 23
Alexander Egger Avatar answered Oct 05 '22 23:10

Alexander Egger