简体   繁体   中英

how to write httphandler to only intercept files in a specific folder

I have written a httphandler to intercept all pdf files request via URL from a specific folder and redirect the user to Login page. If the user is authenticated the file can be downloaded. My web.config has the following entry for the interception

<httpHandlers>
 <add verb="*" path="/calderdale/*.pdf"
   type="NES.HiLo.Security.CalderDaleAuthenticationHandler, NES.HiLo.Security" />
</httpHandlers>

In IIS (6.0) Application extension settings I have added a setting with executable C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_isapi.dll and extension Pdf.

This works but every pdf file request is getting intercepted rather the the files present in calderdale directory.

I have tried solutions given on this link Mapping specific folder to HttpHandler in web.config and removed the application extension setting but then handler does get call at all.

Any ideas?

I think the trick here is to only register the HttpHandler for that specific directory. This can be done by moving the system.web/httpHandlers content into a location node in your web.config. For instance:

<location path="calderdale">  

( system.web/httpHandlers content )

</location>

Add this to your main web config:

<location path="static">
  <system.web>
    <httpHandlers>
      <add verb="GET,HEAD" path="*.*" 
          type="NES.HiLo.Security.CalderDaleAuthenticationHandler, NES.HiLo.Security" />
    </httpHandlers>
  </system.web>    
</location>

Why do you need to do this in that way? Wouldn't it be easier to just use ASP.NET provider and protect this folder with required login? A simple webconfig with logic to access/deny would solve your issue. When someone tries to browse that directory, it will be impossible to download the file and will be redirected to the login page. Let me know if I misunderstood something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM