AuthenticateRequest event
Asked Answered
S

2

45


Q 1. To my understanding FormsAuthenticationModule is subscribed to AuthenticateRequest event, and thus only after this event is fired, is FormsAuthenticationModule called. But the following quotes got me a bit confused:

  1. The AuthenticateRequest event signals that the configured authentication mechanism has authenticated the current request.

    • Doesn’t the above quote suggest that when AuthenticateRequest event is raised, request (aka user) is already authenticated?
  2. Subscribing to the AuthenticateRequest event ensures that the request will be authenticated before processing the attached module or event handler.

    • As far as I understand this quote, if we subscribe to AuthenticatedRequest, then our event handler will be called prior to FormsAuthenticationModule? Thus Application_AuthenticateRequest() will be called before FormsAuthenticationModule is called?


Q 2. Book I’m learning from suggests that within Application_AuthenticateRequest() we are able to verify whether user is a member of specific role, and if not, we can add the user automatically:

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
            if (User.Identity.IsAuthenticated && Roles.Enabled)
            {

                //here we can subscribe user to a role via Roles.AddUserToRole()
            }       
    }

Judging from the above code, Application_AuthenticateRequest() is called after FormsAuthenticationModule has been invoked, but somewhere else same book implies that Application_AuthenticateRequest() is called prior to FormsAuthenticationModule:

Application_AuthenticateRequest is called just before authentication is performed. This is a jumping-off point for creating your own authentication logic.


What am I missing?


Thanx

Stolen answered 17/5, 2009 at 20:53 Comment(0)
C
56

It seems that the FormsAuthenticationModule gets handled first. This module is normally earlier than any custom module in the ASP.NET pipeline, so when AuthenticateRequest is fired, FormsAuthenticationModule will get called first, do its job and then your module's event handler will be called.

If you really want to dig deep into this, I suggest trying to debug the ASP.NET code yourself. Here is a post how to set up your VS:

http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx

EDIT: I was able to confirm this behavior by setting up a web project with custom module and event handlers in Global.asax. Take a look at the source code of HttpApplication.InitInternal, the order of initialization is as follows:

  • initialization of integrated modules: FormsAuthenticationModule hooks up to HttpApplication.AuthenticateRequest event
  • initialization of custom modules: custom module hooks up to HttpApplication.AuthenticateRequest event
  • initialization of Global class (global.asax): here we hook up to the AuthenticateRequest event
  • HttpApplication.InitInternal searches for methods on Global class following the specific name pattern (e.g. Application_AuthenticateRequest), matches them to event and hooks up

After the initialization, when the AuthenticateRequest fires, the event handlers are called in the order they where initialized, so:

  • FormsAuthenticationModule.AuthenticateRequest event handler
  • CustomModule.AuthenticateRequest event handler
  • Global.AuthenticateRequest event handler
  • Global.Application_AuthenticateRequest method

Unless I missed something, there is no mechanism for stopping the event handlers to fire, so no matter what the result of FormsAuthenticationModule.AuthenticateRequest, the next handlers will still be called. I hope that helps.

Crenelate answered 17/5, 2009 at 21:40 Comment(5)
Q1 If I understand you correctly, then when AuthenticateRequest fires, FormsAuthenticationModule is called first, then Application_AuthenticateRequest() and only then are custom authentication modules called? Q2 - But what about that MSDN quote (“The AuthenticateRequest event signals that the configured authentication mechanism has authenticated the current request”), which implies that AuthenticateRequest is fired only after FormsAuthenticationModule does its job?Stolen
Detailed answer in the post. Regarding Q2 - I guess it is not entirely true: “The AuthenticateRequest event signals that the configured authentication mechanism has authenticated the current request” - it certainly went through the event handler on FormsAuthenticationModule, but we do not know the result ;)Crenelate
thanx. You've really helped me out with thisStolen
One little note: the FormsAuthenticationModule.Authenticate event (not called AuthenticateRequest) is called [during the Application.AuthenticateRequest event](FormsAuthenticationModule). You can register to that event just as you can to any other, using the predefined names in global.asax.Stubbed
Thank you so much for this post. I expected the HttpApplication class to have an interface where it explicitly defined the Application_AuthenticateRequest() method.Enemy
C
8

If you want access to the User object, I'd suggest you use

protected void Application_Start()
{
    PostAuthenticateRequest += Application_PostAuthenticateRequest;
}

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    if(User.Identity.IsAuthenticated)
    {
        //Do stuff here
    }
}
Cesaro answered 20/2, 2012 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.