Search code examples
c#asp.net.netforms-authenticationhttpmodule

Handling Forms Authentication events in a HTTP Module


I'm creating a HTTP module by implementing IHttpModule and I'd like to handle the Authenticate event, raised by the Forms Authentication module.

The documentation only states how to handle this event from within Global.asax, how can I handle this event from within my HTTP module?


Solution

  • You can handle the AuthenticateRequest event of the HttpApplication that is passed to the Init method of your IHttpModule implementation:

    // IHttpModule.Init
    public void Init(HttpApplication context)
    {
        // subscribe to the AuthenticateRequest event
        context.AuthenticateRequest += this.onApplicationAuthenticateRequest;
    }
    
    private void onApplicationAuthenticateRequest(object sender, EventArgs e)
    {
        // your code goes here
    }
    

    This article has an example of the basic authentication in Web API which uses custom HttpModule, might be helpful.