I am using the new WebView2 control (which is in developer preview) to replace the WebBrowser control in a Windows.Forms application.
The main reason for switching to the WebView2 control is that it is based on Chromium which works with WebRTC, whereas the WebBrowser control is powered by Internet Explorer which does not support WebRTC.
So the problem I am having is finding a way to set a cookie for the url that I want WebView2 to navigate to. In the past, when using WebBrowser, cookies could be set by calling InternetSetCookie before webBrowser.Navigate, but InternetSetCookie only works with Internet Explorer.
The cookie needs to be set for auth on a third party website, i.e. to prove to the website that my app is already logged in (done moments earlier by other parts of my app that are not using WebView2). The app successfully captures the auth cookie in the login response, but I just can't find how to pass the cookie back to the website when navigating with the WebView2 control. The WebView2 control is used to navigate to another page on the same website, where WebRTC is used.
https://github.com/MicrosoftEdge/WebViewFeedback/issues/4 explains that there is no quick mechanism provided for setting cookies in WebView2 yet, but suggests handling the WebResourceRequested event and then setting a cookie by amending the request.Header from inside the WebResourceRequested event handler.
So can anyone explain how to actually get the WebResourceRequested event to fire for a WebView2 please? I have tried this unsuccessfully:
private string myUrl = "https://www.somedomain.com";
private void WebView_CoreWebView2Ready(object sender, EventArgs e)
{
webView.CoreWebView2.AddWebResourceRequestedFilter(myUrl,CoreWebView2WebResourceContext.All);
webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
}
private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
Uri myUri = new Uri(myUrl);
if (myUri.IsBaseOf(e.Request.RequestUri))
{
e.Request.Headers.Add("Cookie", cookieName, authToken);
}
}
The WebResourceRequested event never fires. I have tried getting it to fire by calling WebView2.Navigate, WebView2.CoreWebView2.Navigate and WebView2.Source, but none of them cause the WebResourceRequested event to fire.
The reason I am hooking up the event handler for WebResourceRequested from within WebView_CoreWebView2Ready event is because if you try to hook it up earlier (such as in form load), then CoreWebView2 will be null because it needs more time. I have successfully hooked up other events inside WebView_CoreWebView2Ready and they did fire (such as the NavigationStarting event).
Thanks.