public static ImpersonationContext SetTargetImpersonationContext() { ImpersonationContext ic = m_ImpersonationContext; m_ImpersonationContext = ImpersonationContext.TargetComputer; if (ic != ImpersonationContext.TargetComputer) { using (logX.loggerX.VerboseCall()) { if (ic == ImpersonationContext.TargetSQLServer) { if (m_targetSQLServerImpersionationContext != null) { logX.loggerX.Verbose(string.Format("Leaving Target SQL Server Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); m_targetSQLServerImpersionationContext.Undo(); m_targetSQLServerImpersionationContext.Dispose(); m_targetSQLServerImpersionationContext = null; } } else if (ic == ImpersonationContext.Local) { logX.loggerX.Verbose(string.Format("Leaving Local Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); //if (m_LocalImpersionationContext != null) //{ // m_LocalImpersionationContext.Undo(); // m_LocalImpersionationContext.Dispose(); // m_LocalImpersionationContext = null; //} } if (m_targetIdentity != null) { m_targetImpersionationContext = m_targetIdentity.Impersonate(); logX.loggerX.Verbose(string.Format("Entering Target Computer Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); } else { if (TargetServer != null) { bool results = TargetServer.Bind(); logX.loggerX.Verbose(string.Format("Entering Target Computer Bind Context: {0}", m_targetUserName)); } } } } return(ic); }
public static void RestoreImpersonationContext(ImpersonationContext ic) { using (logX.loggerX.VerboseCall()) { // Reset old if different than new // ------------------------------- if (m_ImpersonationContext == ImpersonationContext.TargetComputer && ic != ImpersonationContext.TargetComputer) { if (m_targetImpersionationContext != null) { logX.loggerX.Verbose( string.Format("Leaving Target Computer Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); m_targetImpersionationContext.Undo(); m_targetImpersionationContext.Dispose(); m_targetImpersionationContext = null; } else { if (TargetServer != null) { logX.loggerX.Verbose(string.Format("Leaving Target Computer Bind Context: {0}", m_targetUserName)); TargetServer.Unbind(); } } } else if (m_ImpersonationContext == ImpersonationContext.TargetSQLServer && ic != ImpersonationContext.TargetSQLServer) { if (m_targetSQLServerImpersionationContext != null) { logX.loggerX.Verbose( string.Format("Leaving Target SQL Server Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); m_targetSQLServerImpersionationContext.Undo(); m_targetSQLServerImpersionationContext.Dispose(); m_targetSQLServerImpersionationContext = null; } } else if (m_ImpersonationContext == ImpersonationContext.Local && ic != ImpersonationContext.Local) { logX.loggerX.Verbose( string.Format("Leaving Local Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); //if (m_LocalImpersionationContext != null) //{ // m_LocalImpersionationContext.Undo(); // m_LocalImpersionationContext.Dispose(); // m_LocalImpersionationContext = null; //} } // Set new if different than old // ----------------------------- if (ic == ImpersonationContext.TargetComputer && m_ImpersonationContext != ImpersonationContext.TargetComputer) { if (m_targetIdentity != null) { m_targetImpersionationContext = m_targetIdentity.Impersonate(); logX.loggerX.Verbose( string.Format("Entering Target Computer Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); } else { if (TargetServer != null) { TargetServer.Bind(); logX.loggerX.Verbose(string.Format("Entering Target Computer Bind Context: {0}", m_targetUserName)); } } } else if (ic == ImpersonationContext.TargetSQLServer && m_ImpersonationContext != ImpersonationContext.TargetSQLServer) { if (m_targetSQLServerIdentity != null) { m_targetSQLServerImpersionationContext = m_targetSQLServerIdentity.Impersonate(); logX.loggerX.Verbose( string.Format("Entering Target SQL Server Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); } else { if (m_UserSQLAuthentication) { logX.loggerX.Verbose("Using SQL Server Credentials"); } else { logX.loggerX.Verbose("Failed to Enter Target SQL Server Impersonation Context"); logX.loggerX.Verbose(string.Format("Using Local User Context for Target SQL Server: {0}", WindowsIdentity.GetCurrent().Name)); } } } else if (ic == ImpersonationContext.Local && m_ImpersonationContext != ImpersonationContext.Local) { logX.loggerX.Verbose( string.Format("Entering Local Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); //if (m_LocalIdentity != null) //{ // m_LocalImpersionationContext = m_LocalIdentity.Impersonate(); // logX.loggerX.Verbose( // string.Format("Entering Local Impersonation Context: {0}", WindowsIdentity.GetCurrent().Name)); //} } m_ImpersonationContext = ic; } }