public static SafeUserTokenHandle LogonAndGetUserPrimaryToken(NetworkCredential credential) { SafeFileHandle token = null; IntPtr primaryToken = IntPtr.Zero; try { if (NativeMethods.RevertToSelf()) { if (NativeMethods.LogonUser(credential.UserName, ".", credential.Password, NativeMethods.LogonType.LOGON32_LOGON_BATCH, NativeMethods.LogonProvider.LOGON32_PROVIDER_DEFAULT, out token)) { var sa = new NativeMethods.SecurityAttributes(); if (NativeMethods.DuplicateTokenEx( token, NativeMethods.Constants.GENERIC_ALL_ACCESS, sa, NativeMethods.SecurityImpersonationLevel.SecurityImpersonation, NativeMethods.TokenType.TokenPrimary, out primaryToken)) { return(new SafeUserTokenHandle(primaryToken)); } else { throw new Win32Exception(); } } else { throw new Win32Exception(); } } else { throw new Win32Exception(); } } finally { if (token != null) { token.Close(); } } }
public static SafeUserTokenHandle LogonAndGetUserPrimaryToken(NetworkCredential credential) { SafeFileHandle token = null; IntPtr primaryToken = IntPtr.Zero; try { if (NativeMethods.RevertToSelf()) { if (NativeMethods.LogonUser(credential.UserName, ".", credential.Password, NativeMethods.LogonType.LOGON32_LOGON_BATCH, NativeMethods.LogonProvider.LOGON32_PROVIDER_DEFAULT, out token)) { var sa = new NativeMethods.SecurityAttributes(); if (NativeMethods.DuplicateTokenEx( token, NativeMethods.Constants.GENERIC_ALL_ACCESS, sa, NativeMethods.SecurityImpersonationLevel.SecurityImpersonation, NativeMethods.TokenType.TokenPrimary, out primaryToken)) { return new SafeUserTokenHandle(primaryToken); } else { throw new Win32Exception(); } } else { throw new Win32Exception(); } } else { throw new Win32Exception(); } } finally { if (token != null) { token.Close(); } } }
public bool Start() { processInfo = new NativeMethods.ProcessInformation(); var startInfo = new NativeMethods.StartupInfo(); var success = false; SafeFileHandle hToken, hReadOut, hWriteOut, hReadErr, hWriteErr, hReadIn, hWriteIn; var securityAttributes = new NativeMethods.SecurityAttributes(); securityAttributes.bInheritHandle = true; success = NativeMethods.CreatePipe(out hReadOut, out hWriteOut, securityAttributes, 0); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); success = NativeMethods.CreatePipe(out hReadErr, out hWriteErr, securityAttributes, 0); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); success = NativeMethods.CreatePipe(out hReadIn, out hWriteIn, securityAttributes, 0); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); success = NativeMethods.SetHandleInformation(hReadOut, NativeMethods.Constants.HANDLE_FLAG_INHERIT, 0); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); // Logon user success = NativeMethods.LogonUser( runSpec.Credentials.UserName, runSpec.Credentials.Domain, runSpec.Credentials.Password, NativeMethods.LogonType.LOGON32_LOGON_BATCH, NativeMethods.LogonProvider.LOGON32_PROVIDER_DEFAULT, out hToken ); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); IntPtr unmanagedEnv; if (!NativeMethods.CreateEnvironmentBlock(out unmanagedEnv, hToken.DangerousGetHandle(), false)) { int lastError = Marshal.GetLastWin32Error(); throw new Win32Exception(lastError, "Error calling CreateEnvironmentBlock: " + lastError); } // Create process startInfo.cb = Marshal.SizeOf(startInfo); startInfo.dwFlags = NativeMethods.Constants.STARTF_USESTDHANDLES; startInfo.hStdOutput = hWriteOut; startInfo.hStdError = hWriteErr; startInfo.hStdInput = hReadIn; success = NativeMethods.CreateProcessWithTokenW( hToken, 0, null, CommandLine(), NativeMethods.CreateProcessFlags.CREATE_UNICODE_ENVIRONMENT, unmanagedEnv, null, ref startInfo, out processInfo ); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); NativeMethods.DestroyEnvironmentBlock(unmanagedEnv); Handle = processInfo.hProcess; startInfo.hStdOutput.Close(); startInfo.hStdError.Close(); startInfo.hStdInput.Close(); StandardOutput = new StreamReader(new FileStream(hReadOut, FileAccess.Read), Console.OutputEncoding); StandardError = new StreamReader(new FileStream(hReadErr, FileAccess.Read), Console.OutputEncoding); StandardInput = new StreamWriter(new FileStream(hWriteIn, FileAccess.Write), Console.InputEncoding) { AutoFlush = true }; WaitForExitAsync(); return success; }