Fire events for requests to static content without

2019-07-11 07:04发布

问题:

I'm looking for a more precise solution to handle requests for static contents by the ASP.NET lifecycle without setting runAllManagedModulesForAllRequests to true. As far as I know is the effect of runAllManagedModulesForAllRequests = "true" that the precondition attribute of each module will be set to "".

The problem:

  • I have to protect static content in a subfolder of a web application against unauthorized access
  • To include requests to those static contents in the ASP.NET lifecycle and therefore having some events fired, I set runAllManagedModulesForAllRequests to true in web.config.
  • Because this solution turns the big wheel and all managed modules are affected for the whole application, I'm looking for a more adapted solution restricted to the subfolder where this behavior is required.
  • I need a solution for IIS6 and II7

Question 1:

The preconditon of which modules have to be resetted (precondition = "") to fire global.asax.cs events (e.g. Application_BeginRequest) for requests for static contents?

Question 2:

Is it possible to limit this request handling to requests to a single subfolder (e.g. perhaps by placing an adapted web.config in this subfolder, tweeking the main web.config, ...)

Any suggestions would be appreciated. Thanks.

回答1:

Have you thought in the direction of registering a custom HttpModule for the right event of global.asax, and then enabling the HttpModule only for the sub directory using location attribute in the main web.config itself? It is just a thought of a possible solution - I havent thought through it..

<location path="subDirectoryPath">
   <system.web>
     <httpmodules>
        <add type="MyCustomModule.Name" name="MyCustomModule" />      
     </httpmodules>
   </system.web>
</location>

EDIT:

You may have to override your web.config, and bring in all the httpModule section in this, and then insert the custom module at the right place, with the right precondition. This is to avoid setting runAllManagedModulesForAllRequests to true