简体   繁体   English

自动选择数字证书对话框

[英]Automating the Choose a digital certificate dialog

I am using WatiN (2.0.10.928) with C# and Visual Studio 2008 to test a SSL secured website that requires a certificate. 我正在使用WatiN(2.0.10.928)与C#和Visual Studio 2008来测试需要证书的SSL安全网站。 When you navigate to the homepage a "Choose a digital certificate" dialog is displayed and requires that you select a valid certificate and click the 'OK' button. 导航到主页时,会显示“选择数字证书”对话框,要求您选择有效的证书并单击“确定”按钮。

I'm looking for a way to automate the certificate selection so that every time a new test or fixture is executed (and my browser restarts) I don't have to manually interfere with the automated test and select the certificate. 我正在寻找一种自动化证书选择的方法,以便每次执行新的测试或夹具(并且我的浏览器重新启动)时,我不必手动干扰自动测试并选择证书。 I've tried using various WatiN Dialog Handler classes and even looked into using the Win32 API to automate this but haven't had much luck. 我尝试过使用各种WatiN Dialog Handler类,甚至考虑使用Win32 API来自动化这个但是运气不好。

I finally found a solution but its adds another dependency to the solution (a third party library called AutoIT). 我终于找到了一个解决方案,但它为解决方案添加了另一个依赖项(一个名为AutoIT的第三方库)。 Since this solution isn't ideal but does work and is the best I could find, I will post the solution and mark it as the answer but I am still looking for an 'out of the box' WatiN solution that is more consistent with the rest of my code and test fixtures. 由于这个解决方案并不理想,但确实有效并且是我能找到的最好的解决方案,我会发布解决方案并将其标记为答案,但我仍然在寻找一个“开箱即用”的WatiN解决方案 ,它与其余的代码和测试夹具。

Thanks for your responses! 谢谢你的回复!

In my situation I have exactly one certificate attached, so I have to pick up the one and only existing on the list, so I have really simple DialogHandler for this - it only clicks on the button if it cans handle the dialog: 在我的情况下,我只附加了一个证书,所以我必须选择一个并且只存在于列表中,所以我有一个非常简单的DialogHandler - 它只能点击按钮,如果它可以处理对话框:

public class CertificateChoosingHandler : BaseDialogHandler
{
    public override bool HandleDialog(Window window)
    {
        new WinButton(1, window.Hwnd).Click();
        return true;
    }

    public override bool CanHandleDialog(Window window)
    {
        return window.StyleInHex == "94C808CC";
    }
} 

AFAIR this solution won't work in Windows 7. AFAIR此解决方案在Windows 7中不起作用。

EDIT: I forgot about something useful. 编辑:我忘了一些有用的东西。 When I found that this solution is not working in Windows 7, I discovered very interesting option in IE Internet Options somewhere in "Custom Level": Don't prompt for client certificate selection when no certificates or only one certificate exists . 当我发现此解决方案在Windows 7中不起作用时,我在“自定义级别”中的IE Internet选项中发现了非常有趣的选项: 当没有证书或仅存在一个证书时,不要提示选择客户端证书 So I have added my site to trusted sites and edited settings, and there is no need now for me to use this DialogHandler , but it still can be used even if no dialog appears. 所以我已将我的网站添加到受信任的站点和编辑的设置,现在我不需要使用此DialogHandler ,但即使没有出现对话框,它仍然可以使用。 If it is not clear, what I wrote, here is how to Enable Prompt for Certificate in Internet Explorer to show certificate dialog. 如果不清楚,我写了什么,这里是如何在Internet Explorer中启用证书提示以显示证书对话框。

I've taken @prostynick's hint and automated it. 我采取了@ prostynick的提示并将其自动化。 Basically, if you ENABLE the setting " Don't prompt for client certificate selection when no certificates or only one certificate exists " in the IE security settings, then the whole dialog doesn't appear (if you only have one or no certificate, that is). 基本上,如果您在IE安全设置中启用“ 当没有证书或仅存在一个证书时不提示客户端证书选择 ”设置,则不会出现整个对话框(如果您只有一个证书或没有证书,那么是)。

So, we just have to make sure that the user has that setting enabled before we initialize your WebBrowser object. 因此,在初始化WebBrowser对象之前,我们必须确保用户已启用该设置。 And since these settings are conveniently stored in the registry, we can do it ourselves, without bothering the user. 由于这些设置可以方便地存储在注册表中,因此我们可以自己完成,而不会打扰用户。 Here's some code that does just that: 这里有一些代码可以做到这一点:

// What this does is changes this setting in Internet Explorer: 
//   Tools -> Internet Options -> Security -> Custom Level -> 
//   Don't prompt for client certificate selection when no certificates 
//   or only one certificate exists -> ENABLE
//
// If you're not convinced that we need this, please reset all the security 
// levels in IE to the default settings, comment out this code, and try to fetch 
//    <your url>. 
//
// If it finishes, great! Then leave it commented out. Otherwise, curse and accept
// that we need this ugly hack OR that we need to instruct people to find & change 
// some unholy IE setting...
RegistryKey stupidBrokenDefaultSetting = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", true);
stupidBrokenDefaultSetting.SetValue("1A04", "0", RegistryValueKind.DWord);

I'm not sure if this works for everyone, or that you need Administrator rights or something, but it works for me. 我不确定这是否适用于所有人,或者您是否需要管理员权限或其他内容,但它对我有用。

The best solution I could find so far was posted here: http://andrey-zhukov.blogspot.com/2009/10/recently-i-wanted-to-choose-digital.html 到目前为止我能找到的最佳解决方案发布在这里: http//andrey-zhukov.blogspot.com/2009/10/recently-i-wanted-to-choose-digital.html

As stated in the post, it requires a reference to the AutoIT library: http://www.autoitscript.com/autoit3/index.shtml 如帖子中所述,它需要引用AutoIT库: http ://www.autoitscript.com/autoit3/index.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM