2016-03-31 94 views
1

我们有asp.net MVC &角应用程序。我们正在使用identityserver3来对应用程序进行访问控制。 除了一件事外,一切都按预期工作。 未经授权的用户仍然可以访问应用程序的静态内容。阻止访问asp.net的静态内容 - mvc应用程序

在用户登录之前是否有任何方式拒绝访问这些文件?

回答

0

这里是链接到伟大的职位而导致我的解决方案=> Intercepting file requests

步骤我已经采取了解决我的问题:

  1. 将此行添加到我的webconfig文件中。 这将确保js文件请求不会被处理程序处理。

    <system.webServer> 
        <handlers> 
         <add name="JSFileHandler" path="*.js" verb="GET" 
          type="System.Web.Handlers.TransferRequestHandler"  
          preCondition="integratedMode,runtimeVersionv4.0" /> 
        </handlers> 
    </system.webServer> 
    
  2. 注册路线。

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    
        routes.RouteExistingFiles = true; 
        routes.MapRoute(
         "staticfiles" 
         , "{*src}" 
         , new { Controller = "Main", action = "GetJS" } 
         , new { src = @"(.*?)\.(js)" }      // URL constraints 
        ); 
    
  3. 从控制器动作返回文件

    public ActionResult GetJS() 
    { 
    
        var path = this.Url.RouteUrl("staticfiles"); 
        return File(path, 
         System.Net.Mime.MediaTypeNames.Application.Octet, 
         Path.GetFileName(path)); 
    } 
    
1

您可以添加到您的web.config

<location path="your/path/tostaticfiles">  
    <system.web> 
     <authorization>     
     <deny users="?" /> //Denies unauthorized users 
     </authorization> 
    </system.web> 
</location> 
+0

使用表单认证它的作用就像一个魅力,但在这种情况下,它似乎没有任何效果。 – piowtarn

+0

此外,这只适用于在IIS处理程序映射中将类型映射到.NET的文件。例如,jpg文件不是默认的,IIS会在将请求路由到.NET之前提供它们。 – esmoore68

0

除了位置部分,您还需要指出的是IIS ASP.NET将处理这些文件(runAllManagedModulesForAllRequests = “真” )。

下一步(的System.Web节点的兄弟姐妹):

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

在system.webServer节点:

<modules runAllManagedModulesForAllRequests="true"> 

注: “?” 使用用户= “*”,而不是用户=如果你不想让任何用户访问你的文件。在我的情况下,我做到了这一点,以防止访问我的JS文件,我用捆绑服务他们。

相关问题