首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >忽略控制台应用程序中的web浏览器SSL安全警报

忽略控制台应用程序中的web浏览器SSL安全警报
EN

Stack Overflow用户
提问于 2013-07-17 10:59:34
回答 2查看 5.2K关注 0票数 3

我正在创建一个控制台应用程序,可以远程捕获网站的截图。除了我无法避免证书错误这一事实外,一切都在工作。每次我收到我无法通过的弹出信息。

我试着用:

ServicePointManager.ServerCertificateValidationCallback =新RemoteCertificateValidationCallback(ValidateServerCertificate);

但不起作用。也尝试了在这里找到的解决方案:http://www.codeproject.com/Articles/31163/Suppressing-Hosted-WebBrowser-Control-Dialogs,但是它似乎不适用于从控制台应用程序调用的for浏览器。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2013-07-17 15:29:52

webbrowser控件使用WinInet作为其网络堆栈。设置ServerCertificateValidationCallback对WinInet没有任何影响。

要处理证书错误,您需要实现一个IHttpSecurity服务,并根据请求传递给and浏览器。The浏览器通过在IServiceProvider主机上实现的ActiveX查询主机服务。假设您使用的是Windows窗体,则需要执行以下操作:

  • 从WebBrowser派生类
  • 创建从WebBrowser.WebBrowserSite派生的嵌套类(从嵌套类派生的唯一方法)
  • 覆盖CreateWebBrowserSiteBase并返回new浏览器站点的新实例。
  • 在the浏览器站点上实现IServiceProvider
  • 实现IServiceProvider.QueryService,以便在请求IHttpSecurity服务时返回IHttpSecurity imepleemntation
  • 处理IHttpSecurity.OnSecurityProblem并返回S_OK
  • 在表单中使用新的new浏览器

示例代码:

代码语言:javascript
运行
复制
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;
        }
    }
}
票数 4
EN

Stack Overflow用户

发布于 2014-01-08 23:09:19

终于弄明白了。

对于作为控制台应用程序运行的无头IE浏览器(http://triflejs.org),我一直试图绕过SSL证书错误。

ShengJiang提供了大部分答案,但我仍然不能使用Application.Run(),因为它锁定了主线程上的执行,我需要在循环中执行其他事件,而且用消息泵实例化ApplicationContext似乎太复杂了。

一旦我得到了答案,那是非常简单的。只需创建一个循环并运行Application.DoEvents()

这是一些工作代码。我把它简化了,以适应这里张贴的问题。

请确保:

  1. 您正在作为控制台应用程序项目运行。
  2. 将项目引用添加到System.Windows.Forms

希望能帮上忙!

代码语言:javascript
运行
复制
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace IgnoreSSLErrorBrowserConsoleApp
{
    public class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            MyWebBrowser browser = new MyWebBrowser();
            browser.Navigate("about:blank");
            browser.DocumentCompleted += delegate (object obj, WebBrowserDocumentCompletedEventArgs e) {
                if (e.Url.ToString() == "about:blank") {
                    // This is the SSL path where certificate error occurs
                    browser.Navigate("https://localhost");
                }
            };
            while (browser.ReadyState != WebBrowserReadyState.Complete) {
                Application.DoEvents();
                // RunOtherEvents();
            }

        }
    }

    [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;
            }
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17698002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档