Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Last active January 4, 2016 19:29
Show Gist options
  • Select an option

  • Save DinisCruz/8667697 to your computer and use it in GitHub Desktop.

Select an option

Save DinisCruz/8667697 to your computer and use it in GitHub Desktop.
TM Scripts to execute on load
if (TMEvents.OnApplication_BeginRequest.size() > 1)
TMEvents.OnApplication_BeginRequest.remove(1);
TMEvents.OnApplication_BeginRequest.Add(
()=>{
if(HttpContextFactory.Request.Url.str().contains(".js",".ashx",".asmx","jpg",".gif").isFalse())
{
HttpContextFactory.Response.Write("<h1>Custom code 123</h1>");
}
});
return TMEvents.OnApplication_BeginRequest.size();
//using TeamMentor.CoreLib;
//O2Ref:TeamMentor.CoreLib.dll
<%@ Page Language="C#" Trace="false" %>
<%@ Import Namespace ="O2.Kernel" %>
<%@ Import Namespace ="O2.DotNetWrappers.ExtensionMethods" %>
<%@ Import Namespace ="System.Security.Principal" %>
<%@ Import Namespace="TeamMentor.CoreLib" %>
<%
//AdminAttribute.GlobalDisableFor_AdminAttribute = true;
//Response.Write("AdminAttribute.GlobalDisableFor_AdminAttribute: {0} <br/>".format(AdminAttribute.GlobalDisableFor_AdminAttribute));
Response.Write("<h1>Auth Tests 3</h1>");
var identity = WindowsIdentity.GetCurrent();
Response.Write(identity + "<br>");
Response.Write(identity.Name + "<br>");
Response.Write("{0} - {1}<br>".format(identity.IsAuthenticated, identity.ImpersonationLevel));
var windowsAuth = new WindowsAuthentication();
var authGuid = windowsAuth.authenticateUserBaseOn_ActiveDirectory();
Response.Write("authenticateUserBaseOn_ActiveDirectory: {0}<br/>".format(authGuid));
Response.Write("calculateUserGroupBasedOnWindowsIdentity: {0}<br/>".format(windowsAuth.calculateUserGroupBasedOnWindowsIdentity()));
Response.Write("authGuid.validSession(): {0}<br/>".format(authGuid.validSession()));
var tmWebServices = new TM_WebServices();
tmWebServices.tmAuthentication.mapUserRoles(true);
tmWebServices.tmAuthentication.sessionID = authGuid ;
//tmWebServices.tmAuthentication.sessionID = "111c4858-c6b8-4553-bf57-848d25b3f111".guid();
Response.Write("tmWebServices.tmAuthentication.sessionId: {0}<br/>".format(tmWebServices.tmAuthentication.sessionID));
Response.Write("tmWebServices.Current_User(): {0}<br/>".format(tmWebServices.Current_User()));
if (tmWebServices.Current_User()!=null)
{
Response.Write("tmWebServices.Current_User().UserName: {0}<br/>".format(tmWebServices.Current_User().UserName));
}
%>
//return WindowsIdentity.GetCurrent().ImpersonationLevel == TokenImpersonationLevel.Impersonation;
if (TMEvents.OnApplication_BeginRequest.size() > 1)
TMEvents.OnApplication_BeginRequest.remove(1);
TMEvents.OnApplication_BeginRequest.Add(
()=>{
if(HttpContextFactory.Request.Url.str().contains(".js",".ashx",".asmx","jpg",".gif").isFalse())
{
var identity = WindowsIdentity.GetCurrent();
var response = HttpContextFactory.Response;
if(identity.IsAuthenticated && identity.ImpersonationLevel == TokenImpersonationLevel.Impersonation)
{
//"test Info message".info();
response.Write(TM_UserData.Current.newUser("testUser"));
response.Write("<h1>with Windows user</h1>");
//HttpContextFactory.Response.Write("<h1>Custom code 1234</h1>");
}
else
response.Write("<h1>no windows user: {0} - {1} </h1>".format(identity.IsAuthenticated, identity.ImpersonationLevel));
}
});
return TMEvents.OnApplication_BeginRequest.size();
//using System.Security.Principal;
//using TeamMentor.CoreLib;
//O2Ref:TeamMentor.CoreLib.dll
var debugMsg = "...";
var tmWebServices = new TM_WebServices();
Action mapUser =
()=>{
// tmWebServices = new TM_WebServices(false);
var name = WindowsIdentity.GetCurrent().Name;
if (name.isNull())
{
debugMsg = "WindowsIdentity name was null";
return;
}
if (tmWebServices.Current_User().isNull() || name != tmWebServices.Current_User().UserName)
{
if (name.tmUser().isNull())
{
var newUserId = TM_UserData.Current.newUser(name);
"created a new user: {0}".info(newUserId);
}
var tmUser = name.tmUser();
tmUser.FirstName = "AdTest";
tmUser.AccountStatus.ExpirationDate = DateTime.Now.AddDays(40);
tmUser.GroupID = 1;
tmUser.saveTmUser();
var loginGuid = tmUser.login();
var tmAuthentication = tmWebServices.tmAuthentication;
tmAuthentication.sessionID = loginGuid;
debugMsg = "New user created {0} : {1} ".format(tmAuthentication.sessionID == loginGuid, tmWebServices.Current_User());
}
else
debugMsg = "Current user: {0} ".format(tmWebServices.Current_User().UserName);
};
//return debugMsg;
//******
//var windowsAuth = new WindowsAuthentication();
//return windowsAuth.Identity.Name;
//var authGuid = windowsAuth.authenticateUserBaseOn_ActiveDirectory();
//tmWebServices.tmAuthentication.mapUserRoles(true);
//return tmWebServices.Current_User();
//return tmWebServices;
//return authGuid;
if (TMEvents.OnApplication_BeginRequest.size() > 1)
TMEvents.OnApplication_BeginRequest.remove(1);
TMEvents.OnApplication_BeginRequest.Add(
()=>{
if(HttpContextFactory.Request.Url.str().contains(".js",".ashx",".asmx","jpg",".gif").isFalse())
{
try
{
mapUser();
}
catch(Exception ex)
{
debugMsg = "ERROR" + ex.Message;
}
// HttpContextFactory.Response.Write("<h1>[" + tmWebServices + "]</h1>");
// HttpContextFactory.Response.Write("<h1>[" + tmWebServices.Current_User() + "]</h1>");
HttpContextFactory.Response.Write("<b>[" + debugMsg + "]</b></br/>");
}
});
return TMEvents.OnApplication_BeginRequest.size();
//using System.Security.Principal;
//using TeamMentor.CoreLib;
//O2Ref:TeamMentor.CoreLib.dll
//O2Ref:System.Web.Services.dll
var application = HttpContextFactory.Context.ApplicationInstance;
var tmWebServices = new TM_WebServices();
var debugMsg = "...";
Action mapUser =
()=>{
var name = WindowsIdentity.GetCurrent().Name;
if (name.isNull())
{
debugMsg = "WindowsIdentity name was null";
return;
}
if (tmWebServices.Current_User().isNull() || name != tmWebServices.Current_User().UserName)
{
if (name.tmUser().isNull())
{
var newUserId = TM_UserData.Current.newUser(name);
debugMsg += "[created a new user: {0} : {1}]".format(newUserId, name);
}
else
debugMsg += "[user existed: {0}]".format(name);
var tmUser = name.tmUser();
tmUser.FirstName = "AdTest";
tmUser.AccountStatus.ExpirationDate = DateTime.Now.AddDays(40);
tmUser.GroupID = 1;
tmUser.saveTmUser();
var loginGuid = tmUser.login();
var tmAuthentication = tmWebServices.tmAuthentication;
tmAuthentication.sessionID = loginGuid;
debugMsg = "user {0} : {1}".format(tmAuthentication.sessionID == loginGuid, tmWebServices.Current_User());
}
else
debugMsg = "Current user: {0} ".format(tmWebServices.Current_User().UserName);
};
//*** sets the callback
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));
mapUser();
HttpContextFactory.Response.Write("<b>[{0}]</b><br/>".format(debugMsg));
}
};
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";
//using System.Web;
//using System.ComponentModel;
//using System.Security.Principal;
//using TeamMentor.CoreLib;
//O2Ref:TeamMentor.CoreLib.dll
//O2Ref:System.Web.Services.dll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment