public static void CreateProcessAsUser(Win32Logon logon, string cmdline, bool wait = false) { if (!logon.UserLogged) { throw new InvalidOperationException("User not logged-on."); } IntPtr token = logon._impersonate; PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); try { SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES(); sa.Length = Marshal.SizeOf(sa); STARTUPINFO si = new STARTUPINFO(); si.cb = Marshal.SizeOf(si); si.lpDesktop = "winsta0\\default"; IntPtr lpEnvironment; if (!CreateEnvironmentBlock(out lpEnvironment, token, false)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateEnvironmentBlock"); } Trace.TraceInformation("CreateProcessAsUser - Cmdline: ", cmdline); if (!CreateProcessAsUser( token, null, cmdline, ref sa, ref sa, false, 0x00000400, lpEnvironment, null, ref si, ref pi)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateProcessAsUser"); } // block until process exit if (wait) { WaitForSingleObject(pi.hProcess, int.MaxValue); } } finally { if (pi.hProcess != IntPtr.Zero) { CloseHandle(pi.hProcess); } if (pi.hThread != IntPtr.Zero) { CloseHandle(pi.hThread); } } }
public Impersonator() { User = new Win32Logon(); string username = CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountUsername"); string passhash = CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountEncryptedPassword"); if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(passhash)) { throw new ConfigurationErrorsException( "The Impersonator is specifically written for use in Microsoft Azure Cloud Services and requires that Remote Access is configured."); } string password = Secret.Decrypt(passhash); User.Logon(username, password, Environment.MachineName); // User.LoadProfile(); }
public void Dispose() { User.Dispose(); User = null; }
public CommandInvoker() { User = new Win32Logon(); }