We had an application that was happily using version 69.0.0 of the CefSharp.Winforms nuget package.
The original code looked much like this:
`namespace ChromiumTest
{
public partial class Form1 : Form
{
private const string Url = "https://blarblarblar.com";
public Form1()
{
InitializeComponent();
Cef.Initialize(new CefSettings());
CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
var browser = new ChromiumWebBrowser(Url)
{
RequestHandler = new CustomRequestHandler()
};
var x = new RequestContext();
Controls.Add(browser);
}
}
// This CustomerRequestHandler works with v69
public class CustomRequestHandler : DefaultRequestHandler
{
public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request,
IRequestCallback callback)
{
var headers = request.Headers;
headers["Token"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
request.Headers = headers;
return CefReturnValue.Continue;
}
}
}`
Obviously I've hidden the actual token and Url. When this page loads, every single resource request hits the OnBeforeResourceLoad() override and inserts the Token into the headers before continuing to load the resource. All resources from the initial page, and also all resources followed by clicking links -- as this is required
This works fine - and still does, if I roll my Nuget package back to 69.0.0
However, if I move forward to 71.0.0 or above right up to the current version 81.3.100 this doesn't appear work.
I am aware, that some breaking changes happened, and OnBeforeResourceLoad() was moved from the ResourceHandler. So with this in mind, the new code looks like this:
`namespace ChromiumTest
{
public partial class Form1 : Form
{
private const string Url = "https://blarblarblar.com";
public Form1()
{
InitializeComponent();
Cef.Initialize(new CefSettings());
CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
var browser = new ChromiumWebBrowser(Url)
{
RequestHandler = new NewCustomRequestHandler()
};
var x = new RequestContext();
Controls.Add(browser);
}
}
public class NewCustomRequestHandler : RequestHandler
{
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame,
IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
return new CustomResourceRequestHandler();
}
}
public class CustomResourceRequestHandler : ResourceRequestHandler
{
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request,
IRequestCallback callback)
{
var headers = request.Headers;
headers["Token"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
request.Headers = headers;
return CefReturnValue.Continue;
}
}
}`
As you can see I create a NewCustomRequestHandler inheriting from RequestHandler, I override the GetResourceRequestHandler() method, which returns a new instance of CustomResourceRequestHandler, which in turn overrides the default OnBeforeResourceLoad()
As far as I can tell from the documentation, the samples project and posts on StackOverflow this is the pattern required.
When I run this, for the initial page load it works, the overriden OnBeforeResourceLoad() is called for every single request on the intial page.
HOWEVER, as soon as I click a link to go to another page on the same domain, OnBeforeResourceLoad() doesn't appear to fire any more?
In the 69.0.0 version of the code, when I clicked on the same link it was firing!
Anyone have any ideas on how to get this behavour back?
Thank you
Brian
We had an application that was happily using version 69.0.0 of the CefSharp.Winforms nuget package.
The original code looked much like this:
`namespace ChromiumTest
{
public partial class Form1 : Form
{
private const string Url = "https://blarblarblar.com";
}`
Obviously I've hidden the actual token and Url. When this page loads, every single resource request hits the OnBeforeResourceLoad() override and inserts the Token into the headers before continuing to load the resource. All resources from the initial page, and also all resources followed by clicking links -- as this is required
This works fine - and still does, if I roll my Nuget package back to 69.0.0
However, if I move forward to 71.0.0 or above right up to the current version 81.3.100 this doesn't appear work.
I am aware, that some breaking changes happened, and OnBeforeResourceLoad() was moved from the ResourceHandler. So with this in mind, the new code looks like this:
`namespace ChromiumTest
{
public partial class Form1 : Form
{
private const string Url = "https://blarblarblar.com";
}`
As you can see I create a NewCustomRequestHandler inheriting from RequestHandler, I override the GetResourceRequestHandler() method, which returns a new instance of CustomResourceRequestHandler, which in turn overrides the default OnBeforeResourceLoad()
As far as I can tell from the documentation, the samples project and posts on StackOverflow this is the pattern required.
When I run this, for the initial page load it works, the overriden OnBeforeResourceLoad() is called for every single request on the intial page.
HOWEVER, as soon as I click a link to go to another page on the same domain, OnBeforeResourceLoad() doesn't appear to fire any more?
In the 69.0.0 version of the code, when I clicked on the same link it was firing!
Anyone have any ideas on how to get this behavour back?
Thank you
Brian