如何通过自定义httpmodule进行MVC身份验证

时间:2010-11-10 18:15:56

标签: model-view-controller httpmodule

拥有遗留应用程序,然后从那里调用我的MVC应用程序。 我打算使用自定义httpmodule [AuthenticationModule类继承IHttpModule]。在Init中,我连接了BeginRequest并完成了FormAuthenication ......

     private void Application_BeginRequest(Object source, EventArgs e) {

         // Do my own authetication and issue FormAuthentication Ticket
    }

在web.config中:

    <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="AuthenticationModule" type="RealProperty.LegacySecurity.AuthenticationModule, RealProperty.LegacySecurity" preCondition="ManagedHandler"/>
    </modules>

但是我的AuthenticationModule在调试中从未受到过攻击...... (1)任何人都可以解释为什么没有被召唤? (2)在BeginRequest中进行身份验证是否正确?

1 个答案:

答案 0 :(得分:2)

确保Web.config文件中包含以下两个部分:

<system.web>
  <httpModules>
    <add name="MyAuthenticationModule" type="Ns.To.MyAuthenticationModule, MyAssembly" />
  </httpModules>
</system.web>

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="MyAuthenticationModule" type="Ns.To.MyAuthenticationModule, MyAssembly" />
  </modules>
</system.webServer>