Search code examples
wpfwinapiwindows-authenticationcredentials

is it possible to fill in a windows credential prompt programmatically?


Issues of robustness, stability and you shouldn't do this aside, has anyone ever filled in a windows credential prompt via code (so that's one that looks like this:)

windows security prompt

Is it possible to interact with these dialog boxes through Win32 APIs, or using SendKeys/send mouse / UI Automation? Any ideas / tips anyone has would be greatly appreciated!


Solution

  • I ended up using the UI Automation framework, which allowed me to grab a reference to the credential prompt and then fill it out and complete it that way.

    Code snippet:

    AutomationElement desktop = AutomationElement.RootElement;
    //get all windows on the desktop
    AutomationElementCollection windows = desktop.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
             foreach (AutomationElement window in windows)
            {
                if (window.Current.ClassName.Equals("#32770"))   //security dialog
                {
    
                    // access the appropriate AutomationElements to enter credentials here
    
                }
            }
    

    To interact with an element, you grab the appropriate Pattern object and call its methods (eg Textboxes have a ValuePattern which has a .SetValue() method.

    I also used UISpy to find all the values for things like ClassNames, AutomationIds, etc to help find the correct item through .FindAll() and PropertyConditions objects.