Check static file during Application_BeginRequest?

I have a Global.asx file that needs to do user authentication, auditing, and profiling. This is necessary because it supports SAML-based single sign-on and requires overriding regular .Net authentication (which does not support SAML or mixed authentication).

I do not want to run it for static files like .js , .css , .png , etc.

In Cassini / WebDev and IIS7 it does.

What I want to have is a simple check, for example this.Request.IsStaticFile (which, unfortunately, does not exist) to identify static files.

I understand that it would be quite simple to write, but it looks like it should already exist - IIS has already applied caching policy stuff for static files, etc.

I need a solution for the code, not an IIS configuration change.

Update

This is my current solution:

 /// <summary>Hold all the extensions we treat as static</summary> static HashSet<string> allowedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".js", ".css", ".png", ... }; /// <summary>Is this a request for a static file?</summary> /// <param name="request">The HTTP request instance to extend.</param> /// <returns>True if the request is for a static file on disk, false otherwise.</returns> public static bool IsStaticFile(this HttpRequest request) { string fileOnDisk = request.PhysicalPath; if (string.IsNullOrEmpty(fileOnDisk)) { return false; } string extension = Path.GetExtension(fileOnDisk); return allowedExtensions.Contains(extension); } 

It works fast enough, but feels awfully awkward. In particular, relying on extensions, you will be prone to errors if we add new static files that we did not think about.

Is there a better way without changing the IIS configuration?

+6
source share
3 answers

You might be able to check which handler is dealing with the request.

In IIS6, only .net files, such as aspx, are mapped to a handler that does things.

In IIS7 with an integrated pipeline, everything goes through .net, which is usually good. However, different handlers still deal with different types of files. In particular, I believe that staticfilehandler is the one you need to check. The httpcontext.handler property should let you understand this.

You can create an extension method to add this IsStatic method ...

Simon

0
source

There are several options:

  • Adding authorization element and forbid anyone for those paths for which you do not need authentication, and contains your static files.
  • You are using an integrated pipeline . Turn it off on your IIS 7.
0
source

There is no doubt that you need to create your own extension method, because the ASP.NET routing mechanism uses this code to decide if a file exists,

 if (!this.RouteExistingFiles) { string appRelativeCurrentExecutionFilePath = httpContext.Request.AppRelativeCurrentExecutionFilePath; if (((appRelativeCurrentExecutionFilePath != "~/") && (this._vpp != null)) && (this._vpp.FileExists(appRelativeCurrentExecutionFilePath) || this._vpp.DirectoryExists(appRelativeCurrentExecutionFilePath))) { return null; } } 

You cannot decide if the request is static in Application_BeginRequest using context.handler, because the routing module can change the handler, and this module is always executed after Application_BeginRequest. My suggestion is to use the same code that uses the ASP.NEt routing mechanism.

0
source

Source: https://habr.com/ru/post/892666/


All Articles