How does Global.asax PostAuthenticateRequest event binding happen?
Asked Answered
C

1

9

How can I use the PostAuthenticateRequest event of Global.asax? I'm following this tutorial and it mentions that I have to use the PostAuthenticateRequest event. When I added the Global.asax event it created two files, the markup and the code-behind file. Here is the content of the code-behind file

using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace authentication
{
    public class Global : System.Web.HttpApplication
    {    
        protected void Application_Start(object sender, EventArgs e)
        {    
        }

        protected void Session_Start(object sender, EventArgs e)
        {    
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {    
        }

        protected void Application_Error(object sender, EventArgs e)
        {    
        }

        protected void Session_End(object sender, EventArgs e)
        {    
        }

        protected void Application_End(object sender, EventArgs e)
        {    
        }
    }
}

Now when I type the

protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)

It is successfully called. Now I want to know how is the PostAuthenticateRequest bound to this Application_OnPostAuthenticateRequest method? How can I change the method to some other?

Capua answered 13/1, 2011 at 7:51 Comment(0)
V
16

Magic..., a mechanism called Auto Event Wireup, the same reason you can write

Page_Load(object sender, EventArgs e) 
{ 
} 

in your code-behind and the method will automatically be called when the page loads.

MSDN description for System.Web.Configuration.PagesSection.AutoEventWireup property:

Gets or sets a value indicating whether events for ASP.NET pages are automatically connected to event-handling functions.

When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname(), such as Page_Load() or Page_Init(). ASP.NET first looks for an overload that has the typical event-handler signature (that is, it specifies Object and EventArgs parameters). If an event handler with this signature is not found, ASP.NET looks for an overload that has no parameters. More details in this answer.

If you wanted to do it explicitly you would write the following instead

public override void Init()
{
    this.PostAuthenticateRequest +=
        new EventHandler(MyOnPostAuthenticateRequestHandler);
    base.Init();
}

private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}
Violaceous answered 13/1, 2011 at 7:59 Comment(3)
I wasted an hour because it wasn't showing up in Intellisense and I thought I might have to subscribe to the event somehow. Was about to post asking how to implement the event but then I thought let's give it a try and see if I get any errors and voila! it worked :) Thanks anywaysCapua
beware that ie. Application_Start or Session_Start can ONLY be handled through the Auto Event Wireup mechanism, there are no explicit events for them on the HttpApplication class that you can subscribe to.Peccavi
Behind the scenes, the event is hooked up by HttpApplicationFactory.ReflectOnMethodInfoIfItLooksLikeEventHandler.Viniferous

© 2022 - 2024 — McMap. All rights reserved.