Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Global.asax PostAuthenticateRequest event binding happen?

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?

like image 495
Tux Avatar asked Jan 13 '11 07:01

Tux


People also ask

What is Application_BeginRequest event in the global ASAX?

Application_BeginRequest: Fired when an application request is received. It is the first event fired for a request, which is often a page request (URL) that a user enters. Application_EndRequest: The last event fired for an application request.

Which event handler can be included in global ASAX?

Application_Error() – fired when an error occurs. Session_End() – fired when the session of a user ends. Application_End() – fired when the web application ends. Application_Disposed() - fired when the web application is destroyed.

What is Application_AcquireRequestState?

Application_AcquireRequestState. Occurs when ASP.NET acquires the current state (for example, session state) that is associated with the current request. Application_AuthenticateRequest. Occurs when a security module has established the identity of the user.


1 Answers

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)
{
}
like image 146
Pauli Østerø Avatar answered Oct 05 '22 03:10

Pauli Østerø