About event hooking

Last week I was working on this really useful feature that is MembershipProvider. I had to write my own implementation since I'm working on a custom database schema. I began to write my own provider, and decided to test it.

Easy, I put a Login control on my login page, set some properties, and launch the web site to see if it's working. Ok, it cannot work the first time, so I made little changes. Among them, I hooked a method to the Authenticate event to check if it at least passed through it.

It was executing it, but it still wouldn't execute any code in my provider!?

I continued to make some changes in config. It should work, but nothing.

Guess what ! When you hook the Authenticate event, the Login control stops doing its default behavior - that is authenticating the user with the specified Membership provider - and let you do all the work yourself ! That's why nothing happened, and I spent one day and a half on this!

This means that the OnAuthenticate methods that raise the events should look something like this:

    protected bool OnAuthenticate(AuthenticateEventArgs e)

    {

 

        if (Authenticate != null)

        {

            Authenticate(this, e);

            return e.Authenticated;

        }

        else

            return DefaultAuthenticate();

    }

It seams a bit error prone to me... what about adding a boolean Handled property to the EventArg class to say that you did it on your own? Changing the behavior of a component just because you hooked an event is a good way to drive developpers crazy...

Do you know other events with this behavior in the framework?

posted on Tuesday, October 03, 2006 4:44 PM

Feedback

# re: About event hooking 9/11/2007 5:58 PM Balamurugan Shlok

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool blnAuthenticated = true;
blnAuthenticated = SiteLevelCustomAuthencation(Login1.UserName, Login1.Password);
e.Authenticated = blnAuthenticated;
if (blnAuthenticated == true)
{
Response.Redirect("default.aspx");
}

}
private bool SiteLevelCustomAuthencation(string strUsername, string strPwd)
{
bool blnReturnValue = false;

if (strUsername == "bala" && strPwd == "T")
blnReturnValue = true;
return blnReturnValue;

}

# re: About event hooking 4/20/2008 8:02 PM hi

yes!

Title
 
Name
 
Url
Comments   
Protected by Clearscreen.SharpHIPEnter the code you see: