【问题标题】:is it possible to fill in a windows credential prompt programmatically?是否可以以编程方式填写 Windows 凭据提示?
【发布时间】:2012-04-20 03:37:08
【问题描述】:
除了鲁棒性、稳定性和你不应该这样做的问题之外,有没有人通过代码填写过 Windows 凭据提示(所以看起来像这样:)
是否可以通过 Win32 API 或使用 SendKeys/发送鼠标/UI 自动化与这些对话框进行交互?任何人的任何想法/提示将不胜感激!
【问题讨论】:
标签:
wpf
winapi
windows-authentication
credentials
【解决方案2】:
我最终使用了UI Automation framework,它允许我获取对凭据提示的引用,然后填写并以这种方式完成。
代码sn-p:
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
}
}
要与元素交互,您需要获取适当的 Pattern 对象并调用其方法(例如,文本框有一个 ValuePattern,它有一个 .SetValue() 方法。
我还使用 UISpy 查找 ClassNames、AutomationIds 等的所有值,以帮助通过 .FindAll() 和 PropertyConditions 对象找到正确的项目。