从 Web 浏览器控件打开证书信息

发布于 2024-08-30 05:00:03 字数 60 浏览 7 评论 0原文

有谁知道如何从 WebBrowser 控件打开基于 SSL 的“证书信息”屏幕?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲娇萝莉攻 2024-09-06 05:00:03

这可以通过使用名为 X509Certificate2UI 的类来实现。

要使此类可供您使用,您需要添加对 System.Security.dll 的引用

X509Certificate2UI 类中,您有一个名为 DisplayCertificate()< 的方法/code> 接受 X509Certificate2 对象作为参数。调用时,此方法会显示一个对话框,其中显示所有证书信息(包括链接),与 IE 中的对话框完全相同。

Web浏览器控件只能返回一个X509Certificate,然后可以将其传递到X509Certificate2类的构造函数中。

所以代码看起来像这样:

//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);

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);
就像说晚安 2024-09-06 05:00:03

如果我理解正确,您应该不在 WebBrowser 中搜索此信息,而是在 CryptoAPI 中搜索。 Cryptui.dll 中存在诸如 CryptUIDlgSelectCertificateFromStoreCryptUIDlgViewContext 之类的函数。 WINTRUST.DLL中有一些函数,例如WinVerifyTrustEx,也可以显示一些对话框。

您能否准确描述我如何在 Internet Explore 中显示您想要的对话框?您是否已使用 WebBrowser 控件工作,那么您可以在 BeforeNavigate2 事件内部跟踪 Internet Explorer 具有的 url。有了这个 URL,您就可以下载 SSL 证书并在 CryptUIDlgViewContext 方面进行显示。要下载或获取证书,您可以使用带有 INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT 或 INTERNET_OPTION_CLIENT_CERT_CONTEXT 标志的 InternetQueryOption。可以是来自 INTERNET_OPTION_SECURITY_CERTIFICATE、INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT 的信息(请参阅 http://support.microsoft.com/kb/ 251347)对你来说就足够了。

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.

如果没有 2024-09-06 05:00:03

虽然它不使用 .NET WebBrowser,但您可以针对标准 WebBrowser 利用此 C# 包装器代码,而不会对您的项目产生太大影响:

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

然后,您只需说 ShowCertificateDialog()

就无法执行 ExecWB 或调用否则该对话框。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文