public void Logoff(ConnectionDetails connection, int sessionId)
 {
     using (ImpersonationHelper.Impersonate(connection))
     {
         using (var server = GetServer(connection.Server))
         {
             server.Open();
             server.GetSession(sessionId).Logoff();
         }
     }
 }
 public ConnectionState GetSessionState(ConnectionDetails connection, int sessionId)
 {
     using (ImpersonationHelper.Impersonate(connection))
     {
         using (var server = GetServer(connection.Server))
         {
             server.Open();
             return server.GetSession(sessionId).ConnectionState;
         }
     }
 }
 public static IDisposable Impersonate(ConnectionDetails connection)
 {
     var token = IntPtr.Zero;
     if (
         LogonUser(connection.Username, connection.Domain, connection.Password, LogonType.Interactive,
                   LogonProvider.Default, ref token) == 0)
     {
         throw new Win32Exception();
     }
     return new ImpersonationContext(token);
 }
 public bool SessionExists(ConnectionDetails connection, int sessionId)
 {
     using (ImpersonationHelper.Impersonate(connection))
     {
         using (var server = GetServer(connection.Server))
         {
             server.Open();
             try
             {
                 var session = server.GetSession(sessionId);
                 // Windows XP sometimes connects you to session 0, and that session still exists after
                 // logging the user off, as a disconnected console session but with no associated username.
                 return (session.ConnectionState != ConnectionState.Disconnected ||
                         !string.IsNullOrEmpty(session.UserName));
             }
             catch (Exception)
             {
                 return false;
             }
         }
     }
 }
 public MessageBoxShower(RemoteDesktopTestService service, int sessionId, ConnectionDetails connection,
                         string title, string text, RemoteMessageBoxButtons buttons, TimeSpan timeout)
 {
     _service = service;
     _sessionId = sessionId;
     _connection = connection;
     _title = title;
     _text = text;
     _buttons = buttons;
     _timeout = timeout;
 }
 public void StartShowingMessageBox(ConnectionDetails connection, int sessionId, string windowTitle, string text,
                                    RemoteMessageBoxButtons buttons, TimeSpan timeout)
 {
     new MessageBoxShower(this, sessionId, connection, windowTitle, text, buttons, timeout).Show();
 }