经过一番调查,我最终放弃了注册表设置。似乎成功连接的关键是 HKCU\Comm\EAP\Config[SSID] 中的密码值。但是由于 CryptProtectData 使用未记录的熵值(出于明显的安全原因)来加密密码,因此似乎不可能以编程方式在注册表中重新创建有效条目。
然后我选择了第二个最佳解决方案,在调用后捕获用户登录对话框WZCSetInterface
并在其中输入必填字段:
bool enteredPeapCred = false;
DateTime timePeapCredStarted = DateTime.Now.AddSeconds(10);
// wait for PEAP credentials window to appear (max. wait for 10 seconds)
while (!enteredPeapCred && timePeapCredStarted >= DateTime.Now)
{
IntPtr hwndLogon = Win32.FindWindow(null, "User Logon");
if (hwndLogon != IntPtr.Zero)
{
// move User Logon window offscreen to prevent screen flicker in app
Win32.MoveWindow(hwndLogon, -600, 0, 320, 480, true);
// "Network Log On" label
IntPtr hwndCtrl1 = Win32.GetWindow(hwndLogon, Win32.GW_CHILD);
// "Enter network info..." label
IntPtr hwndCtrl2 = Win32.GetWindow(hwndCtrl1, Win32.GW_HWNDNEXT);
// "User name:" label
IntPtr hwndCtrl3 = Win32.GetWindow(hwndCtrl2, Win32.GW_HWNDNEXT);
// username textbox
IntPtr hwndCtrl4 = Win32.GetWindow(hwndCtrl3, Win32.GW_HWNDNEXT);
// "Password:" label
IntPtr hwndCtrl5 = Win32.GetWindow(hwndCtrl4, Win32.GW_HWNDNEXT);
// password textbox
IntPtr hwndCtrl6 = Win32.GetWindow(hwndCtrl5, Win32.GW_HWNDNEXT);
// enter password into textbox
StringBuilder sbPassword = new StringBuilder();
sbPassword.Append(eapPassword);
Win32.SetWindowText(hwndCtrl6, sbPassword);
// "Domain:" label
IntPtr hwndCtrl7 = Win32.GetWindow(hwndCtrl6, Win32.GW_HWNDNEXT);
// domain textbox
IntPtr hwndCtrl8 = Win32.GetWindow(hwndCtrl7, Win32.GW_HWNDNEXT);
// "Save password" checkbox
IntPtr hwndCtrl9 = Win32.GetWindow(hwndCtrl8, Win32.GW_HWNDNEXT);
// send BST_CHECKED message to set checkbox
Win32.SendMessage(hwndCtrl9, Win32.BM_SETCHECK, Win32.BST_CHECKED, 0);
// send WM_COMMAND with left softkey to submit user dialog
IntPtr hwndMenu = Win32.SHFindMenuBar(hwndLogon);
Win32.SendMessage(hwndLogon, Win32.WM_COMMAND, 0x2F87, hwndMenu.ToInt32());
enteredPeapCred = true;
}
}
请注意,我只设置密码字段,因为用户名和域字段已预先填充了已存储在注册表中的信息(我的原始问题中提到的 Identity 值)。
这很好用,因为它使用 PEAP 凭据创建 WLAN 连接。通过在找到用户登录对话框后立即将其移出屏幕,这一切对我们应用程序的用户来说都是不可见的(我们的应用程序在 kiosk 模式下运行)。