ASP.NET HttpModules & Server.Transfer / Server.TransferRequest / RewritePath problems
I will try to be as specific as I can be. I inherited a very antiquated C++ ISAPI filter that secures a classic ASP website and was tasked with the job of creating an HTTPModule to directly replace it.
First I hooked into the OnPreRequestHandlerExecute event. I then successfully recreated the calls to the stored procs that the original ISAPI filter made. Those calls return a statusCode which I toss into a CASE statement and depending on the status code, set a "redirection" string variable (sUrl). For example statusCode 15 will set sUrl to "/Session/Login.asp". This all works successfully.
The next step is the actual "redirect" of the page. In C++ the module was doing the following:
Headers.push_back(HeadersList::value_type("url", "/Session/Errors/SecurIDRequired.asp"));
I am attempting to recreate this functionality by using the following:
HttpContext.Current.RewritePath(sUrl);
This works 100% for all classic asp pages. The problem creeps up when the user trys to access "http://somedomain.com/Blah.jpg. The module successfully runs, sets the proper statusCode of 15, and the RewritePath method is called, but does not rewrite the URL to the login page. The same thing happens for any non classic ASP page.
So I started looking at other options instead of RewritePath and looked at TransferRequest. This looked promising so I changed the code to:
HttpContext.Current.Server.TransferRequest(sUrl, true);
This now routes all requested file types to the login page but bizarre things occur. First off I get the following client side errors in FireBug:
syntax error https://somedomain.com/JS/jQuery/jquery.js Line 2
... some other javascript errors that revolve around jQuery. I look at the Net panel, and it successfully loaded the jQuery library so I am at a loss with this (this does not happen with the RewritePath method).
Finally, I attempted to use good old Server.Transfer and got this server side error:
No http handler was found for request type 'GET'
Phew! I hope my explanation was adequate :)
I guess my real question is this, what is the best way to accomplish what I a开发者_如何学Cm attempting to accomplish. I feel as though the RewritePath method is the best, any tips would be greatly appreciated.
Cheers!
EDIT:
The handlers contained within the web.config are as follows:
<httpHandlers>
<remove verb="*" path="*.asmx" />
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpHandlers>
I don't know if this is the best way to go, but as a test does something like the following work?
In your HttpModule:
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpContext context = ((HttpApplication)source).Context;
HttpRequest request = context.Request;
if(request.Url.ToString().Contains("blah.jpg"))
{
context.RewritePath("~/login.aspx?");
}
}
It sounds like IIS is not handing the requests for images over to ASP.Net, which is the default (IIS just serves up images usually without handing it over to a Page or anything).
Have you tried mapping the image extensions to your custom handler, either in IIS or using the web.config file?
Man, I am so sorry to waste everyone's time.
It turns out that the underlying data structure had some issues (permissions were not set correctly). Due to the complexity of the implemented security model, I must have missed that over and over again until finally I realized the problem. I must have looked at it 100 times and not realized it!
Thanks for all the replies and sorry again guys.
精彩评论