any support available, or in the works, for inserting “partial” into Global.asax.cs System.Web.HttpApplication derived type implementation

Aug 13, 2011 at 4:17 PM

i'm working on a federated identity scaffolding package for use in any project environment where ws-federation federated identity security has been enabled.   This involves adding a boiler plate type implementaiotn to App_Code and a bunch of boiler plate event handlers to Global.asax.cs.  

In an effort to keep it separated from Global.asax.cs so as to better facilitate NuGet package install/update/uninstall what i really want/need is the ability to place it all in a Global.asax.FedId.cs and have that enabled by modifying the original Global.asax.cs  System.Web.HttpApplication derived type implementation to have “partial” inserted upon install and removed upon uninstall, e.g. in case of mvc3 project “public class MvcApplication : System.Web.HttpApplication -> “public partial class MvcApplication : System.Web.HttpApplication”.  

Even better would be a story for having my NuGet package do that and in addition when adding the Content/Global.asax.FedId.cs source file to the csproj file it would be great if it was added as follows <Compile Include="Global.asax.FedId.cs"><DependentUpon>Global.asax</DependentUpon></Compile> with the DependentUpon element so that you get the expected vstudio solution view when looking for code behind files for Global.asax.

Note that I did investing trying to use [assembly: WebActivator.PostApplicationStartMethod(typeof(MvcApplication1.MvcApplication), "PostApplication_Start")] as a way of enabling the federated identity handlers typically placed in Global.asax.cs but the windows identity foundation (wif) plumbing calls these events at interesting times and with/without certain related types initialized and so wiring them up in WebActivator enabled Pre/PostApplication_Start isn’t helping me in this case.

Aug 13, 2011 at 4:45 PM
Edited Aug 13, 2011 at 4:45 PM

WebActivator is normally the solution to these types of scenarios. Did you try both the PreAppStart and PostAppStart events, and neither worked? Can you give more details about what the issue? i.e. what's the correct order of event in which you want things to happen?

Aug 13, 2011 at 5:34 PM

Yes as noted at the end of the post i did try using WebActivator PreAppStart/PostAppStart for doing this and could not make it work since hooking up the wif event handlers is not something you explicitely do.  It looks for them and if found calls them.

In application start i need to wire up "FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;" which i worked around by just calling that event handler in WebActivator PostAppStart using the following code but i'm not sure if this event handler fires again at other times outside of just AppStart processing.
object sender = HttpContext.Current.ApplicationInstance; 
ServiceConfigurationCreatedEventArgs e = new ServiceConfigurationCreatedEventArgs(FederatedAuthentication.SessionAuthenticationModule.ServiceConfiguration);
( HttpContext.Current.ApplicationInstance as MvcApplication).OnServiceConfigurationCreated(sender, e);

In addition to wiring up OnServiceConfigurationCreated i need the following in place which i wasn't able to wire all up PostAppStart and for those i could their implementation is nastier since i then don't have this(HttpAppication) to directly refer to.
void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
protected void Application_Error()
void WSFederationAuthenticationModule_RedirectingToIdentityProvider(object sender, RedirectingToIdentityProviderEventArgs e)
void WSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender, SessionSecurityTokenCreatedEventArgs e)
void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)

Aug 13, 2011 at 6:05 PM

Please try the following approach:

  • In  WebActivator PreStart, register a module using this technique
  • In your module init, you'll receive the HttpApplication, and can use it to register for all the events you care about.

Will this work?

Aug 13, 2011 at 8:51 PM

i don't think so, this gives me an alternate way to access the HttpApplication instance versus PostStart use of HttpContext.Current.   The issue is that with the exception of OnServiceConfigurationCreated none of the other federated identity handler methods are one you add to an event just define them in your Global.asax.cs HttpApplication implementation and the wif sdk routines find and call them when appropriate.   Perhaps if i was a guru on the inner workings of the federated identity wif sdk i'd know how to enable them in other ways.   The approach i'm looking to use also has the upside that it matches what dev's who add this package would be used to seeing in federated identity wif sdk samples and docs.  

If no existing NuGet declarative way for doing this exists is there a Tools\install.ps1 set of steps i could use to slip the "partial" keyword into the Global.asax.cs HttpApplication derived type declaration and Tools\uninstall.ps1 set of steps to remove it?

Aug 14, 2011 at 2:01 AM

All of the things that you want are almost certainly events that you can subscribe to, and that is generally the way HttpApplication works. e.g you list Application_Error/Application_PostAuthenticateRequest, and these are just standard HttpApplication events (see MSDN). I'm not familiar with WIF, but those other things are very likely events as well. e.g. a search for RedirectingToIdentityProvider turns up this event.

Generally, the Foo_Bar type of methods that you are using are just syntactic sugar above events. In fact, some people prefer not to use those because they are too magical and provide no intellisense, nor any way to catch at compile time that you are using the wrong name.

Aug 14, 2011 at 2:04 AM

Another way to look at this is the generally, anything you can do from global.asax can be done from an HttpModule. So NuGet/WebActivator aside, you just need to figure out how to hook up to what you need from a traditional module, and you'll have everything you need.

Aug 19, 2011 at 2:06 AM

Thanks, using the WebActivator PreStart to register a module enabled assignment of all the handlers i needed to register in an App_Start\MyPackage.cs.pp implementation.