Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Last active August 29, 2015 13:56
Show Gist options
  • Save DinisCruz/8806307 to your computer and use it in GitHub Desktop.
Save DinisCruz/8806307 to your computer and use it in GitHub Desktop.
Setting HttpHandler dynamically
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var fieldInfo = (System.Reflection.FieldInfo)application.type().BaseType.BaseType.field("_initInternalCompleted");
PublicDI.reflection.setField(fieldInfo, application, false); //need to set this or we get an 'Event handlers can only be bound to HttpApplication events during IHttpModule initialization.' on the application.PostAuthorizeRequest below
//return PublicDI.reflection.getFieldValue(fieldInfo, application); //usually true
try
{
application.PostAuthorizeRequest += (object sender, EventArgs e) => { "in PostAuthorizeRequest".info(); };
}
catch(Exception ex)
{
return ex.Message;
}
return application;
//The PostAuthorizeRequest didn't get a callback (shown above, but the BeginRequest below did)
//it might be that we need to do something else when there isn't at least one Event Mapped by default
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var fieldInfo = (System.Reflection.FieldInfo)application.type().BaseType.BaseType.field("_initInternalCompleted");
PublicDI.reflection.setField(fieldInfo, application, false); //need to set this or we get an 'Event handlers can only be bound to HttpApplication events during IHttpModule initialization.' on the application.PostAuthorizeRequest below
//return PublicDI.reflection.getFieldValue(fieldInfo, application); //usually true
try
{
application.BeginRequest += (object sender, EventArgs e) =>
{
if(HttpContextFactory.Request.Url.str().contains(".js",".ashx",".asmx","jpg",".gif").isFalse())
{
HttpContextFactory.Response.Write("<b>[BeginRequest]</b>");
}
};
}
catch(Exception ex)
{
return ex.Message;
}
return application;
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var fieldInfo = (System.Reflection.FieldInfo)application.type().BaseType.BaseType.field("_initInternalCompleted");
PublicDI.reflection.setField(fieldInfo, application, false); //need to set this or we get an 'Event handlers can only be bound to HttpApplication events during IHttpModule initialization.' on the application.PostAuthorizeRequest below
//return PublicDI.reflection.getFieldValue(fieldInfo, application); //usually true
Action<String> showCurrentUser =
(callbackName)=>{
if(HttpContextFactory.Request.Url.str().contains(".js",".ashx",".asmx","jpg",".gif").isFalse())
{
var message = "in {0} the current windows user is {1}"
.format(callbackName,WindowsIdentity.GetCurrent().Name);
HttpContextFactory.Response.Write("<b>[{0}]</b><br/>".format(message));
}
};
application.AuthenticateRequest += (object sender, EventArgs e) => { showCurrentUser("AuthenticateRequest" ); };
application.BeginRequest += (object sender, EventArgs e) => { showCurrentUser("BeginRequest" ); };
application.PostAuthenticateRequest += (object sender, EventArgs e) => { showCurrentUser("PostAuthenticateRequest" ); };
application.AcquireRequestState += (object sender, EventArgs e) => { showCurrentUser("AcquireRequestState" ); };
//AcquireRequestState is the one we need, since by then the Windows Username has been resolved
return "mappings added";
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var eventObject = typeof(HttpApplication).fieldValue("AcquireRequestState");
var listEntry = eventHandlerList.invoke("Find",eventObject);
var handler = listEntry.field("handler");
var invocationList = (Delegate[])handler.invoke("GetInvocationList");
var invocationTargets = (from item in invocationList select item.Target).toList();
return invocationTargets.asString().contains("DynamicType+");
return application;
return TM_Xml_Database.Current.Path_XmlDatabase;
//using TeamMentor.CoreLib;
//O2Ref:TeamMentor.CoreLib.dll
//using System;
//using System.Web;
//using System.ComponentModel;
//using TeamMentor.CoreLib;
//using O2.DotNetWrappers.ExtensionMethods;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment