1

This is semi-related to a question I opened a while ago. The way the ASP.NET Forms Authentication module behaves is it registers a subscription to app.AuthenticateRequest and app.EndRequest The way forms auth hijacks EndRequest is very poor manners and was a poor design decision making extending Forms Auth excruciatingly complex. Currently I have a nasty implementation that interdicts the redirection to do what I want instead. I've never been pleased with this, random thought occured to me today reviewing this code is there a way I could just unhook

app.EndRequest += new EventHandler(this.OnLeave);

I see that there are potentially relevant answers on How to remove all event handlers from a control but the implementations of these answers seem to vary wildly and appear that they might only be relevant to Control inheritors.

This most important part is to remove 1 specific event subscription, not blanket nuking. I suppose any solution that can get the subscriptions, nuke them all and then allow me to resubsubscribe for all others is just as valid.

Community
  • 1
  • 1
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258

1 Answers1

0

Something like this should do the trick:

var formsModule = application.Modules.OfType<FormsAuthenticationModule>().FirstOrDefault();
if (formsModule != null)
{
   const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
   var method = typeof(FormsAuthenticationModule).GetMethod("OnLeave", flags);
   var handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), formsModule, method);
   application.EndRequest -= handler;
}
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151