-->

Automating the Choose a digital certificate dialog

2020-07-17 15:05发布

问题:

I am using WatiN (2.0.10.928) with C# and Visual Studio 2008 to test a SSL secured website that requires a certificate. 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.

I finally found a solution but its adds another dependency to the solution (a third party library called 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.

Thanks for your responses!

回答1:

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:

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.

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. 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. If it is not clear, what I wrote, here is how to Enable Prompt for Certificate in Internet Explorer to show certificate dialog.



回答2:

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

As stated in the post, it requires a reference to the AutoIT library: http://www.autoitscript.com/autoit3/index.shtml



回答3:

I've taken @prostynick's hint and automated it. 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).

So, we just have to make sure that the user has that setting enabled before we initialize your WebBrowser object. 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.