views:

253

answers:

3

Does anyone know how to open up the "Certificate Information" screen based on the SSL from the WebBrowser control?

+1  A: 

If I understand you correct you should search for this information not in WebBrowser but inside of CryptoAPI. Exist such function like CryptUIDlgSelectCertificateFromStore, CryptUIDlgViewContext from Cryptui.dll. There are some functions in WINTRUST.DLL like WinVerifyTrustEx which can also display some dialogs.

Could you exactly describe how I can display dialog what you want in Internet Explore? Do you works already with WebBrowser control, then you can trace, for example, inside of BeforeNavigate2 Event the url which Internet Explorer has. Having this URL you can download SSL certificate an display if with respect of CryptUIDlgViewContext. To download or get the certificate you can use InternetQueryOption with INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT or INTERNET_OPTION_CLIENT_CERT_CONTEXT flag. It can be that information from INTERNET_OPTION_SECURITY_CERTIFICATE, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, (see http://support.microsoft.com/kb/251347) will be enough for you.

Oleg
@Oleg - Thanks for the interesting information, but this information is all attainable from a `X509Certificate` by using a `HttpWebRequest`. +1 Though.
Kyle Rozendo
+1  A: 

While it's not using the .NET WebBrowser, you could leverage this C# wrapper code against the standard WebBrowser without much impact on your project:

http://code.google.com/p/csexwb2/

It will then require you only to say ShowCertificateDialog()

There is no way to do an ExecWB or invoke that dialog otherwise.

Nissan Fan
@Nissan - Thanks for this, I can't use it currently but it adds an "easy" to implement solution.
Kyle Rozendo
I looked and looked for ways to call Exec and bring up the dialog with the native .NET control using ActiveXInstance but never had any success. I wish you luck on this going forward.
Nissan Fan
@Nissan - Have a look at the new accepted answer. It might give you some joy later on.
Kyle Rozendo
So glad you found a definitive answer to this.
Nissan Fan
+1  A: 

This can be achieved by using a class called X509Certificate2UI.

To make this class avalable to you, you need to add a reference to System.Security.dll

In the X509Certificate2UI class you have a meyhod called DisplayCertificate() which takes an X509Certificate2 object as a parameter. When invoked, this method shows a dialog box displaying all cert information including chaining, exactly the same as the dialog box you will find in IE.

The webbrowser control can only return a X509Certificate which can then be passed into the constructor of the X509Certificate2 class.

So the code looks as such:

//includes on top
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

//Do webrequest to get info on secure site
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://securesite.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();

//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert = request.ServicePoint.Certificate;

//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2 cert2 = new X509Certificate2(cert);

//display the cert dialog box
X509Certificate2UI.DisplayCertificate(cert2);
Yo Momma
This is perfect. This works 100%, and if I could have accepted I would. The accepted answer is from a bounty, so I can't. +1 though.
Kyle Rozendo