本文介绍了忽略控制台应用程序的Web浏览器的SSL安全警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个控制台应用程序,能够远程捕获的网站截图。一切正常,只是我无法回避证书错误的情况。每次我说我无法通过弹出消息。

I'm creating a console application that is able to remotely capture screenshots of websites. Everything is working except the fact that I can't avoid Certificate errors. Every time I get popup message that I'm unable to pass.

我试着使用:

ServicePointManager.ServerCertificateValidationCallback =新RemoteCertificateValidationCallback(ValidateServerCertificate);

但它不工作。
还试图在这里找到了解决办法:

但但它似乎没有对web浏览器从控制台应用程序调用的工作。

But it doesn't work.Also tried the solution found here:http://www.codeproject.com/Articles/31163/Suppressing-Hosted-WebBrowser-Control-Dialogsbut but it doesnt seem to work for webbrowser invoked from console application.

任何想法?

推荐答案

WebBrowser控件使用的WinInet作为其网络协议栈。设置ServerCertificateValidationCallback会对WinInet的任何效果。

The webbrowser control uses WinInet as its network stack. Setting the ServerCertificateValidationCallback would have no effect on WinInet.

要处理证书错误,你需要实现IHttpSecurity服务,并传递给web浏览器的请求。 web浏览器通过查询该IServiceProvider的ActiveX主机上实现主机服务。假设你使用的是Windows窗体,你需要做到以下几点:

To handle the certificate error, you need to implement an IHttpSecurity service and pass to the webbrowser on request. The webbrowser queries host services via IServiceProvider implemented on the ActiveX host. Assuming you are using Windows Forms, you need to do the following:


  • 从web浏览器派生类

  • 创建从WebBrowser.WebBrowserSite派生的嵌套类(可以从嵌套类派生的唯一途径)

  • 覆盖CreateWebBrowserSiteBase并返回你的网页浏览器的网站的新实例。

  • 在web浏览器的网站实现的IServiceProvider

  • 在请求IHttpSecurity服务时实现IServiceProvider.QueryService所以它返回一个IHttpSecurity imepleemntation

  • 处理IHttpSecurity.OnSecurityProblem并返回S_OK

  • 使用新增了WebBrowser形式

举例code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void webBrowser1_DocumentCompleted(object sender, 
        WebBrowserDocumentCompletedEventArgs e)
    {
        if (e.Url.ToString() == "about:blank")
        {
            //create a certificate mismatch
            webBrowser1.Navigate("https://74.125.225.229");
        }
    }
}
[Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface UCOMIServiceProvider
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int QueryService(
        [In] ref Guid guidService,
        [In] ref Guid riid,
        [Out] out IntPtr ppvObject);
}
[ComImport()]
[ComVisible(true)]
[Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IWindowForBindingUI
{ 
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int GetWindow(
        [In] ref Guid rguidReason,
        [In, Out] ref IntPtr phwnd);
}

[ComImport()]
[ComVisible(true)]
[Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IHttpSecurity
{
    //derived from IWindowForBindingUI
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int GetWindow(
        [In] ref Guid rguidReason,
        [In, Out] ref IntPtr phwnd);
    [PreserveSig]
    int OnSecurityProblem(
        [In, MarshalAs(UnmanagedType.U4)] uint dwProblem);
}
public class MyWebBrowser : WebBrowser
{
    public static Guid IID_IHttpSecurity 
        = new Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b");
    public static Guid IID_IWindowForBindingUI 
        = new Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b");        
    public const int S_OK = 0;
    public const int S_FALSE = 1;
    public const int E_NOINTERFACE = unchecked((int)0x80004002);
    public const int RPC_E_RETRY = unchecked((int)0x80010109);
    protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
    {
        return new MyWebBrowserSite(this);
    }
    class MyWebBrowserSite : WebBrowserSite, 
        UCOMIServiceProvider, 
        IHttpSecurity, 
        IWindowForBindingUI 
    {
        private MyWebBrowser myWebBrowser;
        public MyWebBrowserSite(MyWebBrowser myWebBrowser)
            :base(myWebBrowser)
        {
            this.myWebBrowser = myWebBrowser;
        }
        public int QueryService(ref Guid guidService
            , ref Guid riid
            , out IntPtr ppvObject)
        {
            if (riid ==IID_IHttpSecurity)
            {
                ppvObject= Marshal.GetComInterfaceForObject(this
                    , typeof(IHttpSecurity));
                return S_OK;
            }
            if (riid == IID_IWindowForBindingUI)
            {
                ppvObject = Marshal.GetComInterfaceForObject(this
                    , typeof(IWindowForBindingUI));
                return S_OK;
            }
            ppvObject = IntPtr.Zero;
            return E_NOINTERFACE;
        }

        public int GetWindow(ref Guid rguidReason
            , ref IntPtr phwnd)
        {
            if (rguidReason == IID_IHttpSecurity 
                || rguidReason == IID_IWindowForBindingUI)
            {
                phwnd = myWebBrowser.Handle;
                return S_OK;
            }
            else
            {
                phwnd = IntPtr.Zero;
                return S_FALSE;
            }
        }

        public int OnSecurityProblem(uint dwProblem)
        {
            //ignore errors
            //undocumented return code, does not work on IE6
            return S_OK;
        }
    }
}

这篇关于忽略控制台应用程序的Web浏览器的SSL安全警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:31