限制访问共享文件夹

问题描述:

我有一个名为“reports”的共享文件夹。在这个文件夹中有pdf文件。限制访问共享文件夹

fd6.pdf fd3.pdf

在用户 “FD3” 通过验证,用户可以通过URL此文件夹中访问任何PDF。有没有简单的方法来防止这种情况?

+0

asp.net是否运行集成安全性?或者你使用表单认证/定制的东西?您是否尝试删除文件的读取权限? – rene

我想保护的文件与本地属性在web.config中

<location path="pdfs/files"> 
<system.web> 
    <authorization> 
    <allow roles="superuser,admin,VolunteerCoordinator,presenter,ReferralMaker"/> 
    <deny users="*"/> 
    </authorization> 
</system.web> 

然后确保你有你为防止访问您正在保护的目录而定义的角色

有几种方法可以做到这一点。我假定您的身份验证是Forms Auth或其他此类自定义应用程序身份验证,而不是通过模拟进行管理(如果您使用的是Windows身份验证/模拟,那么您可以直接在文件上使用ACL来解决此问题)。

  1. 您需要配置IIS以便它将调用PDF文件的托管模块。如果您使用的是IIS7 +,这很简单,因为您可以使用集成管道。如果您使用的是IIS6,或者必须使用经典管道,那么您需要使用wildcard mapping或至少映射PDF扩展,以允许为您的静态内容执行托管代码。
  2. 创建一个单独的IHttpModule,或添加到您的Web应用程序的Global.asax.cs。你想要做的是附加到应用程序的AuthorizeRequest事件。
  3. 检查路径和文件名以查看它是否符合您的规则。如果失败,请将Response.StatusCode设置为403(HTTP:Forbidden)。

的Global.asax.cs例如:

protected void Application_AuthorizationRequest(object sender, EventArgs e) 
{ 
    var application = (HttpApplication)sender; 
    var context = application.Context; 

    if (context.User.Identity.IsAuthenticated 
     && Path.GetFileName(Path.GetDirectoryName(context.Request.Path)).Equals("reports", StringComparison.OrdinalIgnoreCase) 
     && Path.GetExtension(context.Request.Path).EndsWith("pdf", StringComparison.OrdinalIgnoreCase)) 
    { 
     var fileName = Path.GetFileNameWithoutExtension(context.Request.Path); 

     if (!context.User.Identity.Name.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)) 
     { 
      context.Response.StatusCode = 403; 
      context.Response.StatusDescription = "You are forbidden to view this file."; 
     } 
    } 
}