Hi all,
Is it possible to set cookies for the new WebViewBrowser control?
Up until now I've been using the ChromiumWebBrowser control and I was able to get the cookie manager by means of either
Cef.GetGlobalCookieManager()
or
ChromiumWebBrowser.GetCookieManager()
However, as ChromiumWebBrowser has been deprecated, I have just switched to using the WebViewBrowser control instead and I cannot find a way to set the cookies or access the cookie manager. Is there any way to achieve this?
From what I have gathered, WebView2 allows to achieve this via its property CoreWebView2. The cookie manager can be accessed directly via the property CookieManager.
WebView2.CoreWebView2.CookieManager
Another option is to capture the event WebResourceRequested and attach a header to the CoreWebView2WebResourceRequest provided by CoreWebView2WebResourceRequestedEventArgs.
CoreWebView2WebResourceRequestedEventArgs.Request.Headers.SetHeader()
If it was currently not possible to set the cookies for the WebViewBrowser control, would the ArcGIS Pro SDK 3.0 expose either CoreWebView2 or CookieManager, or perhaps the event WebResourceRequested?
I'm using ArcGIS Pro SDK 2.9.0.32739.
Thanks
Solved! Go to Solution.
I figured out the problem with the last approach. The cookie obtained with HttpCookie.TryParse() had a null domain. The following code works for me:
private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (HttpCookie.TryParse(cookieString, out HttpCookie httpCookie))
{
CoreWebView2Cookie cookie = browser.CoreWebView2.CookieManager.CreateCookie(httpCookie.Name, httpCookie.Value, domain, httpCookie.Path);
cookie.IsHttpOnly = httpCookie.HttpOnly;
cookie.IsSecure = httpCookie.Secure;
browser.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie);
}
}
Hi,
Have you tried this:
It uses CoreWebView2Ready event
Thanks for your reply, Gintautas. The solution suggested is ultimately using the event WebResourceRequested. The other event just tells you when CoreWebView2 is ready to be used.
I cannot use that solution or other solutions applicable to WebView2 because WebViewBrowser is not exposing all WebView2's properties, methods and events. Neither CoreWebView2 nor the event WebResourceRequested are exposed by WebViewBrowser.
There is possibility to do that from the code behind. I have tried to test on WebViewBrowser from ArcGIS community samples but it doesn't work on my computer at all without changes.
Add name and one of events (Initialized or CoreWebView2InitializationCompleted) to frameworkControls:WebViewBrowser:
<frameworkControls:WebViewBrowser x:Name="webView" Grid.Row="1" Grid.ColumnSpan="3" Source="{Binding SourceUri, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Initialized="WebViewBrowser_Initialized" CoreWebView2InitializationCompleted="WebView_CoreWebView2InitializationCompleted"/>
Then create events methods in code behind and add logic from link:
private void WebViewBrowser_Initialized(object sender, EventArgs e)
{
if(webView.CoreWebView2 != null)
{
Debug.WriteLine("not null");
// Implement logic from link
}
}
private void WebView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
if(webView.CoreWebView2 != null)
{
Debug.WriteLine("not null");
// Implement logic from link
}
}
I don't know which event is right because can't test.
can you make sure you have the following assemblies referenced in your addin:
<ArcGIS Pro Install Folder>\bin\Microsoft.Web.WebView2.Core.dll
<ArcGIS Pro Install Folder>\bin\Microsoft.Web.WebView2.Wpf.dll
Then u should be able to access the webView2.CoreWebView2 property (and, from there, the cookie manager once CoreWebView2 is initialized)
From https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Framework#webviewbrowser-control
Hi,
My ArcGIS Pro Community SDK sample has all references, but it does not show page content.
Thanks for your reply, Charles. I was already referencing those assemblies but I was wrong about CoreWebView2. WebViewBrowser is indeed exposing CoreWebView2. I was mislead by the fact that that property is missing from the ArcGIS Pro API Reference.
However, I have tried both approaches described in the original post and none of them work.
The following one doesn't work because the event WebResourceRequested is never raised.
private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
browser.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
}
private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
e.Request.Headers.SetHeader("Cookie", cookieString);
}
And the following one doesn't work either. The destination page displays a login page, suggesting the cookie is not being sent. The problem is not that the cookie is added too late, because if I navigate to the page again I still get the login page.
private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (HttpCookie.TryParse(cookieString, out HttpCookie httpCookie))
{
CoreWebView2Cookie cookie = browser.CoreWebView2.CookieManager.CreateCookie(httpCookie.Name, httpCookie.Value, httpCookie.Domain, httpCookie.Path);
cookie.IsHttpOnly = httpCookie.HttpOnly;
cookie.IsSecure = httpCookie.Secure;
browser.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie);
}
}
I figured out the problem with the last approach. The cookie obtained with HttpCookie.TryParse() had a null domain. The following code works for me:
private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (HttpCookie.TryParse(cookieString, out HttpCookie httpCookie))
{
CoreWebView2Cookie cookie = browser.CoreWebView2.CookieManager.CreateCookie(httpCookie.Name, httpCookie.Value, domain, httpCookie.Path);
cookie.IsHttpOnly = httpCookie.HttpOnly;
cookie.IsSecure = httpCookie.Secure;
browser.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie);
}
}