public CapturedExceptionResultTransporter <bool> GetExplorerUserToken() { try { var explorerProcesses = Process.GetProcessesByName("explorer"); Logger.Debug("Explorer process count: " + explorerProcesses.Count()); foreach (Process explorer in explorerProcesses) { IntPtr userToken = IntPtr.Zero; if (!WindowsAccountServices.GetUserTokenFromProcess(explorer.Id, ref userToken) || userToken == IntPtr.Zero) { Logger.Error("Could not retrieve user token from explorer process " + explorer.Id + ": userToken=" + userToken.ToInt32() + ", error code: " + Marshal.GetLastWin32Error()); throw new Exception("Could not retrieve user token from explorer process " + explorer.Id); } else { Logger.Info("Sucessfully retrieved user token for Explorer process " + explorer.Id); } WindowsAccountServices.CloseHandle(userToken); } return(new CapturedExceptionResultTransporter <bool>(true)); } catch (Exception ex) { return(new CapturedExceptionResultTransporter <bool>(ex)); } }
public CapturedExceptionResultTransporter <bool> GetCurrentUserToken() { try { IntPtr token = new IntPtr(); return(new CapturedExceptionResultTransporter <bool>(WindowsAccountServices.GetSessionUserToken(ref token))); } catch (Exception ex) { return(new CapturedExceptionResultTransporter <bool>(ex)); } }
public CapturedExceptionResultTransporter <bool> StartProcessAsCurrentUser(string expectedUser) { try { var currentnotepadProcesses = Process.GetProcessesByName("notepad"); Logger.Debug("Before running, there were " + currentnotepadProcesses.Count() + " notepad processes"); if (!WindowsAccountServices.StartProcessAsCurrentUser("C:\\WINDOWS\\system32\\notepad.exe")) { throw new Exception("Could not start notepad as current user!"); } var notepadProcesses = Process.GetProcessesByName("notepad"); Logger.Debug("Notepad process count: " + notepadProcesses.Count()); var newNotepadProcesses = notepadProcesses.Where(x => !currentnotepadProcesses.Any(y => y.Id == x.Id)); Logger.Debug("Notepad processes that we appeared to create: " + newNotepadProcesses.Count()); if (newNotepadProcesses.Count() == 0) { throw new Exception("The notepad process doesn't appear to have started!"); } int notepadProcessesByUser = 0; foreach (Process notepadProcess in newNotepadProcesses) { if (GetProcessOwner(notepadProcess.Id) == expectedUser) { notepadProcessesByUser += 1; } notepadProcess.Kill(); } if (notepadProcessesByUser < 1) { throw new Exception("The notepad process did not have the expected user!"); } return(new CapturedExceptionResultTransporter <bool>(true)); } catch (Exception ex) { return(new CapturedExceptionResultTransporter <bool>(ex)); } }