Asp.net

我可以確定 HttpModules 是按照它們在 HttpApplication.Modules 集合中列出的順序執行的嗎?

  • February 20, 2013

我想寫一個IHttpModule必須嚴格執行的FormsAuthenticationModule,否則就沒用了。

HttpContext.Current.ApplicationInstance.Modules返回 s 集合的屬性IHttpModule。我可以檢查我的模組是否FormsAuthenticationModule在此集合中。

這樣就夠了嗎?該集合列表是否IHttpModule按照它們的執行順序排列?

回想一下,模組可以訂閱不同的管道事件。在任何給定的管道事件中,模組應該按照它們在 Modules 集合中指定的順序執行。

作為一個實際範例,想像一個 Modules 集合,其中註冊了這三個模組:

  • 訂閱 EndRequest 的模組 A
  • 訂閱 BeginRequest 和 EndRequest 的模組 B
  • 訂閱 AuthenticateRequest 的模組 C

執行順序將是:

  1. 模組 B,開始請求
  2. 模組 C,AuthenticateRequest
  3. 模組 A,結束請求
  4. 模組 B,結束請求

由於 FormsAuthenticationModule 訂閱了 AuthenticateRequest 事件,請考慮讓您自己的模組訂閱 PostAuthenticateRequest 事件。這樣,您就可以保證如果 FormsAuthenticationModule 邏輯執行,它會在您的 logic 之前執行,而不管它們在 Modules 集合中註冊的順序如何。

引用自:https://stackoverflow.com/questions/14973667