Beispiel #1
0
        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);
                }
            }
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
 public void Dispose()
 {
     User.Dispose();
     User = null;
 }
Beispiel #4
0
 public CommandInvoker()
 {
     User = new Win32Logon();
 }