Call StaticFileHandler
-
19-06-2021 - |
Question
I have an HttpHandler mapped to aspnet_isapi.dll to perform a custom authentication check on static files (.pdf files) using IIS 7.5 in Classic mode:
void IHttpHandler.ProcessRequest(HttpContext context)
{
if(!User.IsMember) {
Response.Redirect("~/Login.aspx?m=1");
}
else {
//serve static content
}
}
The above code works fine, except for the else statement logic. In the else statement, I simply want to allow the StaticFileHandler to process the request, but I haven't been able to sort this out. Any suggestions on how to simply "hand off" the file back to IIS to serve the request as a normal StaticFile request, would be appreciated.
Solution
To answer your question directly, you can create a StaticFileHandler and have it process the request:
// Serve static content:
Type type = typeof(HttpApplication).Assembly.GetType("System.Web.StaticFileHandler", true);
IHttpHandler handler = (IHttpHandler)Activator.CreateInstance(type, true);
handler.ProcessRequest(context);
But a better idea might be to create an HTTP module instead of an HTTP handler:
public class AuthenticationModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication application)
{
application.AuthorizeRequest += this.Application_AuthorizeRequest;
}
private void Application_AuthorizeRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
if (!User.IsMember)
context.Response.Redirect("~/Login.aspx?m=1");
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow