如何在IIS 7.5上使用ASP.NET表单身份验证保护静态文件?

时间:2010-05-25 09:06:31

标签: asp.net authentication iis-7 iis-7.5

我在共享主机上的IIS 7.5服务器上运行ASP.NET 4.0,但是完全信任。

该网站是一个基本的“文件浏览器”,允许访问者登录并显示可用的文件列表,显然,下载文件。静态文件(主要是pdf文件)位于名为data的站点上的子文件夹中,例如, http://example.com/data/ ...

该网站使用ASP.NET表单身份验证。

我的问题是:如何让ASP.NET引擎处理数据文件夹中静态文件的请求,以便ASP.NET对文件请求进行身份验证,并且用户无法深入链接到一个文件和抓取他们不被允许的文件?

最好的问候,埃吉尔。

6 个答案:

答案 0 :(得分:44)

如果应用程序池在集成模式下运行,则可以执行以下操作。

将以下内容添加到顶级web.config。

  <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中的标准ASP.NET权限来强制对目录中的所有文件进行表单身份验证。

<system.web>
    <authorization>
        <deny users="?" />
    </authorization>
    <authentication mode="Forms" />
</system.web>

答案 1 :(得分:13)

我在获取身份验证方面遇到了同样的问题。通过反复试验,我终于得到了@Joel Cunningham代码的小编辑:

<modules runAllManagedModulesForAllRequests="true" >

我使用这两个网站作为参考:http://forums.iis.net/t/1177964.aspxhttp://learn.iis.net/page.aspx/244/how-to-take-advantage-of-the-iis-integrated-pipeline/

答案 2 :(得分:9)

这是一个老线程,但我碰巧遇到了与Egil相同的问题。以下是Joel的修复版本,其中包括角色:

<modules runAllManagedModulesForAllRequests="false">
      <remove name="FormsAuthenticationModule" />
      <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
      <remove name="UrlAuthorization" />
      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"  />
      <remove name="RoleManager" />
      <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
      <remove name="DefaultAuthentication" />
      <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>

答案 3 :(得分:8)

我想知道为什么需要重新添加默认添加的集成管道模块(默认选项),所以我挖了一点。

您需要删除并重新添加模块,因为默认情况下,模块未添加默认选项。为向后兼容性添加了前提条件,仅为已注册的ASP.NET处理程序(例如.aspx页面)处理的内容运行。

默认情况如下:

<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" 
         preCondition="managedHandler" />

通过删除模块并在没有前提条件的情况下重新添加模块,这些单独的模块将针对每个请求(包括静态内容)运行。它比启用runAllManagedModulesForAllRequests更精细。

您可以在使用IIS 7引入集成管道的几篇文章中阅读相关内容:

请注意,第二篇文章中有一个拼写错误或模块名称(以及@ John的答案)在某些时候已从FormsAuthenticationModule更改为FormsAuthentication

IIS 7.5到8.5中的工作模块集对我来说是这样的:

<system.webServer>
  <modules>
    <!-- Re-add auth modules (in their original order) to run for all static and dynamic requests -->
    <remove name="FormsAuthentication" />
    <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
    <remove name="DefaultAuthentication" />
    <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
    <remove name="RoleManager" />
    <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
    <remove name="UrlAuthorization" />
    <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
  </modules>
</system.webServer>

答案 4 :(得分:7)

附录:

正如@eych所说,这也会阻止访问~/Content文件夹(或您拥有CSS的任何地方)和~/Scripts,等等。

如果您想允许例外 - 即允许未经身份验证的用户访问某些文件/文件夹 - 您可以通过location元素执行此操作。将以下内容添加到web.config

  <location path="Content">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

<强>更新 更好的解决方案是默认保持访问 - 这将允许访问您的CSS / JavaScript等 - 并将“锁定”(仅)应用于存储静态内容的文件夹:

<location path="data">
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
</location>

警告:在我们的案例中(MVC网站),我们需要使用[AuthorizeAttribute]来装饰我们所有的控制器操作(登录除外)。无论如何,这是一个好主意,但以前没有必要(因为之前任何未经授权的请求被重定向到登录页面)。

答案 5 :(得分:1)

If you application pool is running in Classic mode, you can do the following. You will have to repeat these steps for each file extension you'd like to handle, but I'm using .html here.

First, add a page build provider to the Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web> 
    <compilation>
      <buildProviders>
        <add type="System.Web.Compilation.PageBuildProvider" extension=".html"/>
      </buildProviders>
    </compilation>
  </system.web> 
</configuration>

Then add a page handler factory:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web> 
    <httpHandlers>
      <add type="System.Web.UI.PageHandlerFactory" path="*.html" verb="*"/>
    </httpHandlers>
  </system.web> 
</configuration>

Then add a page handler:

<?xml version="1.0" encoding="UTF-8"?>
<configuration> 
  <system.webServer>
    <handlers>
      <add scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" path="*.html" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" name="HtmlHandler-Classic-32" />
      <add scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness64" path="*.html" verb="GET,HEAD,POST,DEBUG" name="HtmlHandler-Classic-64"/>
    </handlers>
  </system.webServer>
</configuration>

This worked for me. (Credit: http://www.ifinity.com.au/Blog/EntryId/66/How-To-301-Redirect-htm-or-html-pages-to-DotNetNuke-aspx-pages.)