Skip to content

Commit 647a072

Browse files
committed
ExampleRequestHandler - Improve OnCertificateError example
1 parent b31a365 commit 647a072

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

CefSharp.Example/Handlers/ExampleRequestHandler.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Security.Cryptography.X509Certificates;
7+
using System.Threading.Tasks;
78
using CefSharp.Handler;
89

910
namespace CefSharp.Example.Handlers
@@ -31,23 +32,42 @@ protected override bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowse
3132

3233
protected override bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
3334
{
34-
//NOTE: If you do not wish to implement this method returning false is the default behaviour
35-
// We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource.
36-
//callback.Dispose();
37-
//return false;
35+
//NOTE: We also suggest you wrap callback in a using statement or explicitly execute callback.Dispose as callback wraps an unmanaged resource.
3836

39-
//NOTE: When executing the callback in an async fashion need to check to see if it's disposed
40-
if (!callback.IsDisposed)
37+
//Example #1
38+
//Return true and call IRequestCallback.Continue() at a later time to continue or cancel the request.
39+
//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
40+
Task.Run(() =>
4141
{
42-
using (callback)
42+
//NOTE: When executing the callback in an async fashion need to check to see if it's disposed
43+
if (!callback.IsDisposed)
4344
{
44-
//To allow certificate
45-
//callback.Continue(true);
46-
//return true;
45+
using (callback)
46+
{
47+
//We'll allow the expired certificate from badssl.com
48+
if (requestUrl.ToLower().Contains("https://expired.badssl.com/"))
49+
{
50+
callback.Continue(true);
51+
}
52+
else
53+
{
54+
callback.Continue(false);
55+
}
56+
}
4757
}
48-
}
58+
});
4959

50-
return false;
60+
return true;
61+
62+
//Example #2
63+
//Execute the callback and return true to immediately allow the invalid certificate
64+
//callback.Continue(true); //Callback will Dispose it's self once exeucted
65+
//return true;
66+
67+
//Example #3
68+
//Return false for the default behaviour (cancel request immediately)
69+
//callback.Dispose(); //Dispose of callback
70+
//return false;
5171
}
5272

5373
protected override void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath)

0 commit comments

Comments
 (0)