c#-4.0 - 如何绕过 CefSharp WinForms SSL 错误

标签 c#-4.0 ssl-certificate cefsharp

我正在使用 CefSharp.WinForms 开发应用程序。 当发生任何 SSL 证书错误时,将不会显示网页。

如何绕过 SSL 证书错误并显示网页?

最佳答案

选项 1(首选)

实现IRequestHandler.OnCertificateError - 对于每个无效证书都会调用此方法。如果您只想重写 IRequestHandler 的一些方法,那么您可以继承 RequestHandler覆盖您特别感兴趣的方法,在本例中为OnCertificateError

//Make sure you assign your RequestHandler instance to the `ChromiumWebBrowser`
browser.RequestHandler = new ExampleRequestHandler();

public class ExampleRequestHandler : RequestHandler
{
    protected override bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
    {
        //NOTE: We also suggest you wrap callback in a using statement or explicitly execute callback.Dispose as callback wraps an unmanaged resource.

        //Example #1
        //Return true and call IRequestCallback.Continue() at a later time to continue or cancel the request.
        //In this instance we'll use a Task, typically you'd invoke a call to the UI Thread and display a Dialog to the user
        Task.Run(() =>
        {
            //NOTE: When executing the callback in an async fashion need to check to see if it's disposed
            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    //We'll allow the expired certificate from badssl.com
                    if (requestUrl.ToLower().Contains("https://expired.badssl.com/"))
                    {
                        callback.Continue(true);
                    }
                    else
                    {
                        callback.Continue(false);
                    }
                }
            }
        });

        return true;

        //Example #2
        //Execute the callback and return true to immediately allow the invalid certificate
        //callback.Continue(true); //Callback will Dispose it's self once exeucted
        //return true;

        //Example #3
        //Return false for the default behaviour (cancel request immediately)
        //callback.Dispose(); //Dispose of callback
        //return false;
    }
}
<小时/>

选项 2

设置忽略证书错误命令行参数

var settings = new CefSettings();
settings.CefCommandLineArgs.Add("ignore-certificate-errors");

Cef.Initialize(settings);

关于c#-4.0 - 如何绕过 CefSharp WinForms SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35555754/

相关文章:

angular - 在 CefSharp 中使用 Angular2

c# - 让视频在 CefSharp 中循环播放

c# - 打开另一个窗体后关闭一个窗体

C# 类型或命名空间 Bunifu 找不到

ssl - 私有(private)域 Web 应用程序的 K8 证书颁发机构

python-requests:是否可以绕过 HTTPS 以提高速度

C# 将对象转换为枚举

c# - 运算符 |= 在 C# 中是什么意思?

java - 连接到 google analytics api 时 Java 中的 SSL 异常

c# - 如何将本地 HTML 加载到包含 CSS 样式表的 CefSharp?