<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Paul Mrozowski's Blog - IIS</title>
    <link>http://www.rcs-solutions.com/blog/</link>
    <description>A day in the life (of a developer)</description>
    <language>en-us</language>
    <copyright>Paul Mrozowski / RCS Solutions, Inc.</copyright>
    <lastBuildDate>Wed, 24 Sep 2008 01:00:44 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>paulm@rcs-solutions.com</managingEditor>
    <webMaster>paulm@rcs-solutions.com</webMaster>
    <item>
      <trackback:ping>http://www.rcs-solutions.com/blog/Trackback.aspx?guid=9ceaccd8-befb-4b38-991e-087e3a98a5bb</trackback:ping>
      <pingback:server>http://www.rcs-solutions.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.rcs-solutions.com/blog/PermaLink,guid,9ceaccd8-befb-4b38-991e-087e3a98a5bb.aspx</pingback:target>
      <dc:creator>Paul Mrozowski</dc:creator>
      <wfw:comment>http://www.rcs-solutions.com/blog/CommentView,guid,9ceaccd8-befb-4b38-991e-087e3a98a5bb.aspx</wfw:comment>
      <wfw:commentRss>http://www.rcs-solutions.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9ceaccd8-befb-4b38-991e-087e3a98a5bb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET makes it fairly simple to enable security on content hosted inside of ASP.NET
(ASPX pages, ASHX, etc.) just by enabling form-based authentication. Any attempt to
access those pages will automatically get redirected to a login page for authentication.
However, static content (HTM or HTML pages) for example aren't passed through the
ASP.NET pipeline, so anyone can access that content (unless you've set-up something
like Basic Authentication) - your user's aren't required to authenticate before accessing
the content. 
</p>
        <p>
It turns out it's pretty simple to get IIS to pass requests to ASP.NET and let it
handle any permissions, logging, etc. that you may want to apply - in fact, this is
how ASP.NET itself hooks into IIS. The first thing you need to do is open the IIS
Manager. Right-click on the website and select properties. Click on the "Home Directory"
tab, then click on the Configuration button. That will display a list of application
extensions and the EXE/DLL that is responsible for those filetypes. We're going to
handle any files with a .HTM extension, so click on Add. In the "Executable" field
enter something like "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
- I just copied this from the ASPX extension and pasted it in. In the extension field
enter ".HTM". Leave everything else as-is and click on OK. If you want to map HTML
files as well, repeat this procedure and enter ".HTML" as the extension (w/o the quotes).
Hit OK to all the prompts to close the various dialogs. At this point IIS will forward
requests for files with either .HTM or .HTML files to ASP.NET.  
</p>
        <p>
  
</p>
        <p>
          <a href="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/Properties_2.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="642" alt="Properties" src="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/Properties_thumb.png" width="568" border="0" />
          </a>
        </p>
        <p>
          <a href="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/AddExtMap_4.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="472" alt="AddExtMap" src="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/AddExtMap_thumb_1.png" width="671" border="0" />
          </a>
        </p>
        <p>
Now we need to tell ASP.NET how to handle these file types by editing the web.config
file to add an HTTP handler for these types. In the system.web section, we're going
to map these types to use a built-in handler called "StaticFileHandler". The web.config
will look something like this: 
</p>
        <p>
          <font face="Courier New">&lt;system.web&gt;<br />
   &lt;httpHandlers&gt;<br />
      &lt;add path="*.htm" verb="*" type="System.Web.StaticFileHandler"
/&gt;<br />
      &lt;add path="*.html" verb="*" type="System.Web.StaticFileHandler"
/&gt;</font>
        </p>
        <p>
Save the web.config. At this point requests for .HTM and .HTML will be passed through
ASP.NET and it will pass the request to an instance of the StaticFileHandler class.
If you have form-based security, you will automatically be redirected to log in as
well before being able to view any HTM or HTML pages (assuming you've protected all
pages). The main downside to pushing this through ASP.NET is that IIS can no longer
handle accessing HTM/HTML pages, which will reduce the efficiency and scalability
of the site. However, with most sites this isn't really much of an issue. The other
issue I've noticed is that the StaticFileHandler class automatically sets the caching
of the item served up to 1 day (regardless of what you've configured in IIS), and
there isn't any way of overridding this behavior short of writing your own handler. 
</p>
        <p>
BTW - This can be used to secure other content as well, for example, PDF files, JPG's,
GIF's, etc. You would just need to add an entry in IIS like we did for HTM/HTML files
and make an associated entry in the web.config. In the example above I was specifically
mapping each content type on an as-needed basis. I should also mention that you can
also just use a wildcard mapping in IIS (option below the one we used) which will
map any file types not in the first list. In that case you don't need to modify the
web.config - it should be handled by the DefaultHttpHandler class (ASP.NET 2.0 and
later - this isn't the case for 1.1). 
</p>
        <p>
If you're interesting in writing your own handler for static files, I did some searching
and found <a href="http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx" target="_blank">this</a>.
He includes code for his own implementation of a static file handler which includes
compression and caching. 
</p>
        <p>
  
</p>
        <p>
          <strong>Links</strong>
        </p>
        <a href="http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx">http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx</a>
        <img width="0" height="0" src="http://www.rcs-solutions.com/blog/aggbug.ashx?id=9ceaccd8-befb-4b38-991e-087e3a98a5bb" />
      </body>
      <title>Securing Static Content Through ASP.NET</title>
      <guid isPermaLink="false">http://www.rcs-solutions.com/blog/PermaLink,guid,9ceaccd8-befb-4b38-991e-087e3a98a5bb.aspx</guid>
      <link>http://www.rcs-solutions.com/blog/2008/09/24/SecuringStaticContentThroughASPNET.aspx</link>
      <pubDate>Wed, 24 Sep 2008 01:00:44 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET makes it fairly simple to enable security on content hosted inside of ASP.NET
(ASPX pages, ASHX, etc.) just by enabling form-based authentication. Any attempt to
access those pages will automatically get redirected to a login page for authentication.
However, static content (HTM or HTML pages) for example aren't passed through the
ASP.NET pipeline, so anyone can access that content (unless you've set-up something
like Basic Authentication) - your user's aren't required to authenticate before accessing
the content. 
&lt;/p&gt;
&lt;p&gt;
It turns out it's pretty simple to get IIS to pass requests to ASP.NET and let it
handle any permissions, logging, etc. that you may want to apply - in fact, this is
how ASP.NET itself hooks into IIS. The first thing you need to do is open the IIS
Manager. Right-click on the website and select properties. Click on the "Home Directory"
tab, then click on the Configuration button. That will display a list of application
extensions and the EXE/DLL that is responsible for those filetypes. We're going to
handle any files with a .HTM extension, so click on Add. In the "Executable" field
enter something like "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
- I just copied this from the ASPX extension and pasted it in. In the extension field
enter ".HTM". Leave everything else as-is and click on OK. If you want to map HTML
files as well, repeat this procedure and enter ".HTML" as the extension (w/o the quotes).
Hit OK to all the prompts to close the various dialogs. At this point IIS will forward
requests for files with either .HTM or .HTML files to ASP.NET.&amp;nbsp; 
&lt;p&gt;
&amp;nbsp; 
&lt;p&gt;
&lt;a href="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/Properties_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="642" alt="Properties" src="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/Properties_thumb.png" width="568" border="0"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;a href="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/AddExtMap_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="472" alt="AddExtMap" src="http://www.rcs-solutions.com/blog/content/binary/WindowsLiveWriter/SecuringStaticContentThroughASP.NET_12679/AddExtMap_thumb_1.png" width="671" border="0"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Now we need to tell ASP.NET how to handle these file types by editing the web.config
file to add an HTTP handler for these types. In the system.web section, we're going
to map these types to use a built-in handler called "StaticFileHandler". The web.config
will look something like this: 
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;lt;system.web&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;httpHandlers&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;add path="*.htm" verb="*" type="System.Web.StaticFileHandler"
/&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;add path="*.html" verb="*" type="System.Web.StaticFileHandler"
/&amp;gt;&lt;/font&gt; 
&lt;/p&gt;
&lt;p&gt;
Save the web.config. At this point requests for .HTM and .HTML will be passed through
ASP.NET and it will pass the request to an instance of the StaticFileHandler class.
If you have form-based security, you will automatically be redirected to log in as
well before being able to view any HTM or HTML pages (assuming you've protected all
pages). The main downside to pushing this through ASP.NET is that IIS can no longer
handle accessing HTM/HTML pages, which will reduce the efficiency and scalability
of the site. However, with most sites this isn't really much of an issue. The other
issue I've noticed is that the StaticFileHandler class automatically sets the caching
of the item served up to 1 day (regardless of what you've configured in IIS), and
there isn't any way of overridding this behavior short of writing your own handler. 
&lt;/p&gt;
&lt;p&gt;
BTW - This can be used to secure other content as well, for example, PDF files, JPG's,
GIF's, etc. You would just need to add an entry in IIS like we did for HTM/HTML files
and make an associated entry in the web.config. In the example above I was specifically
mapping each content type on an as-needed basis. I should also mention that you can
also just use a wildcard mapping in IIS (option below the one we used) which will
map any file types not in the first list. In that case you don't need to modify the
web.config - it should be handled by the DefaultHttpHandler class (ASP.NET 2.0 and
later - this isn't the case for 1.1). 
&lt;/p&gt;
&lt;p&gt;
If you're interesting in writing your own handler for static files, I did some searching
and found &lt;a href="http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx" target="_blank"&gt;this&lt;/a&gt;.
He includes code for his own implementation of a static file handler which includes
compression and caching. 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp; 
&lt;p&gt;
&lt;strong&gt;Links&lt;/strong&gt;
&lt;/p&gt;
&lt;a href="http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx"&gt;http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx&lt;/a&gt;&lt;img width="0" height="0" src="http://www.rcs-solutions.com/blog/aggbug.ashx?id=9ceaccd8-befb-4b38-991e-087e3a98a5bb" /&gt;</description>
      <comments>http://www.rcs-solutions.com/blog/CommentView,guid,9ceaccd8-befb-4b38-991e-087e3a98a5bb.aspx</comments>
      <category>ASP.NET</category>
      <category>IIS</category>
    </item>
  </channel>
</rss>