Skip to content

Instantly share code, notes, and snippets.

@thEpisode
Created December 19, 2018 21:21
Show Gist options
  • Save thEpisode/ed0b8d851a15f56b4cfa18736747f6af to your computer and use it in GitHub Desktop.
Save thEpisode/ed0b8d851a15f56b4cfa18736747f6af to your computer and use it in GitHub Desktop.
CefSharp Implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.WinForms;
namespace Skype.FileTransfer.Controls.Utils
{
public static class BrowserLoader
{
public static void Initialize()
{
if (!Cef.IsInitialized)
{
CefSettings settings = new CefSettings();
settings.IgnoreCertificateErrors = true;
settings.CefCommandLineArgs.Add("disable-application-cache", "1");
settings.CefCommandLineArgs.Add("disable-session-storage", "1");
settings.PersistSessionCookies = false;
Cef.Initialize(settings);
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
}
}
public static void ShutDown()
{
if (Cef.IsInitialized)
{
Cef.Shutdown();
}
}
}
}
namespace Skype.FileTransfer.Controls.Utils
{
using CefSharp;
using CefSharp.Handler;
using Controls;
public class RequestHandler : DefaultRequestHandler
{
private string _requestedUrl = "";
private bool _isCredentialsPrompted = false;
public override void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request,
IResponse response, UrlRequestStatus status, long receivedContentLength)
{
var result = _requestedUrl.Contains(request.Url);
base.OnResourceLoadComplete(browserControl, browser, frame, request, response, status, receivedContentLength);
}
public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
if (_isCredentialsPrompted)
{
if (request.Url.EndsWith("errorPage.html"))
{
// Here I need to refresh or delete the cache
string url = this._requestedUrl;
browserControl.Load(url);
}
}
else
{
this._requestedUrl = request.Url;
}
return base.OnBeforeResourceLoad(browserControl, browser, frame, request, callback);
}
public override bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port,
string realm, string scheme, IAuthCallback callback)
{
bool handled = false;
_isCredentialsPrompted = true;
// Instantiate the dialog box
AuthBox dlg = new AuthBox(); // create new dialog with username and password field.
// Open the dialog box modally
dlg.ShowDialog();
if (dlg.DialogResult)
{
// The user did not cancel out of the dialog. Retrieve the username and password.
callback.Continue(dlg.Username, dlg.Password);
handled = true;
}
else
{
var filelocator = new FileLocator();
string url = filelocator.GetFilePath("errorPage.html");
browserControl.Load(url);
}
return handled;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment