Search code examples
c#testingrdpflaui

Type password automatically for RDP connection (CredentialUIBroker)


As an exercise, I'm trying to automate typing RDP credentials using FlaUI. My OS is Windows 10.

I'm able to start mstsc.exe and type into this window:

mstsc window

But then I get this window and I can't find it anywhere:

Credential Manager UI Host window

It's not an mstsc window, even though it appears above it as a modal window: mstsc always has just one window. Apparently it's a window of "Credential Manager UI Host", but that process has... zero windows.

Even in task manager it's listed in the background tasks and not in the applications section. FlaUI Inspect doesn't see it at all.

By the way, this is my code:

var CurrentAutomation = new UIA3Automation();
var Process = Application.Attach(Process.GetProcessesByName("CredentialUIBroker")[0]);
var Windows = Process.GetAllTopLevelWindows(CurrentAutomation); // 0 elements

How can I get a handle to this window and access its textbox using FlaUI?


Solution

  • It turns out it was just a matter of knowing the name of the "window", which is Credential Dialog Xaml Host; also, it can be found using FlaUI Inspect.

    Once the mstsc part is done and the "Windows Security" window comes out, you can go on with this sample code:

    // Declare all variables, which might be method parameters instead
    var Password = "MyLamePassword";
    var MaxTimeout = new TimeSpan(10 * 1000 * 2000);
    var CurrentAutomation = new UIA3Automation();
    var Desktop = CurrentAutomation.GetDesktop();
    
    // Get the window, using a Retry call to wait for it to be available
    var CredentialWindow = Retry
        .WhileEmpty(
            () => Desktop.FindAllDescendants(f => f.ByClassName("Credential Dialog Xaml Host")),
            timeout: MaxTimeout,
            throwOnTimeout: true)
        .Result[0];
    
    // Get the password box
    AutomationElement PasswordBox = null;
    Retry.WhileNull(
        () => PasswordBox = CredentialWindow.FindFirstDescendant(f => f.ByName("Password").And(f.ByControlType(ControlType.Edit))),
        timeout: MaxTimeout,
        throwOnTimeout: true);
    
    // Type the password
    PasswordBox.FocusNative();
    Keyboard.Type(Password);
    
    // I have some Retry code here too, just to check that the password is actually typed, and type Enter after it. 
    
    CurrentAutomation.Dispose();