Can I be sure that HttpModules are executed in the order in which they are listed in the HttpApplication.Modules collection?
.net, asp.net, httpmodule, iis
Solution
Recall that modules can subscribe to different pipeline events. Within any given pipeline event, modules should run in the order in which they're specified in the Modules collection.
As a practical example, imagine a Modules collection which has these three modules registered:
- Module A, which subscribes to EndRequest
- Module B, which subscribes to BeginRequest and EndRequest
- Module C, which subscribes to AuthenticateRequest
The order of execution will be:
- Module B, BeginRequest
- Module C, AuthenticateRequest
- Module A, EndRequest
- Module B, EndRequest
Since the FormsAuthenticationModule subscribes to the AuthenticateRequest event, consider making your own module subscribe to the PostAuthenticateRequest event. That way you're guaranteed that if the FormsAuthenticationModule logic runs, it runs before your logic, regardless of the order in which they're registered in the Modules collection.
Problem
I want to write an `IHttpModule` that has to be executed strictly after `FormsAuthenticationModule`, otherwise it will be useless. There's `HttpContext.Current.ApplicationInstance.Modules` property that returns a collection of `IHttpModule`s. I can check that my module is after `FormsAuthenticationModule` in this collection. Will that be enough? Does that collection list `IHttpModule`s in the order in which they are executed?