-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting UserAgent not effective on first navigation. #2862
Comments
Hi @wilva, |
Hello @ElyssaJyu, That makes sense, thank you. To stay with the example from above. First set the .Source in MainWindow.xaml above to an empty string.
So now no site is loaded on starting the demo application. On running the example again, it does not instantiate the WebView2 control. It usually comes back with "Edge 106" as useragent, but also every now and then with "FooBar". When looking at the events in a debugger it first comes past WebView_CoreWebView2InitializationCompleted and after that it triggers WebView_NavigationStarting. |
FWIW. I'm also seeing this in the Win32 interface where I am setting the UserAgent during creation and then navigate manually from a button. In that case the WebView2 control is created long before navigation happens. |
Hi @wilva, |
Hi @ElyssaJyu, OK, let's remove that debate about what was first triggered. Make the following additional edit:
into
In addition, to remove any doubt, comment out the creation of the async creation of the WebView2 control from the "go" button logic:
Comment out the first line down there. Compile and run the program. Select "Increase Zoom" from the menu. Now add "https://www.whatismybrowser.com" in the URL bar, press "Go". |
Hi @wilva, What mentioned in my first comment about "wait for the initialization to complete to get the property from the CoreWebView2" causes some misunderstanding. |
Hi @ElyssaJyu ,
I do not see setting UserAgent triggering the NavigationStarting event. Following my steps, that I described above, I see the following happen:
Every now and then I see the "FooBar" useragent reported, but most of the time it is the "Edge 106" one (one out of 20 times that I just tested it reported "FooBar") The NavigationStarting event isn't triggered until I press on the "go" button which sends navigate from GoToPageCmdExecuted. It is not triggered by setting the UserAgent. You seem to be saying that it is not possible to have the first navigation of the WebView2 control to have a UserAgent set that is different from the default UserAgent. Also note this other bugreport which might be related. #1861 |
Hi, I kept wondering why you keep on insisting that your link negated my point of view as -when I read it- it actually seems to confirm my view. Turns out that the example WebView2WpfBrowser had changed since I had last downloaded it, I can see how that makes it a bit more confusing. The StartPageUri logic did not exist in my version... so I updated to the version from today and my notes from above mostly still apply.
into:
This is so there is NO navigation on creating the WebView2 object, which is crucial to my report. After that I still see everything I still reported earlier today. Hope this helps. |
Let me organize: What I replied above does not try to negate your point, but to explain why it cannot be effective on the first navigation and works on the second time.
|
Thank you @ElyssaJyu. So is it not possible for the WebView2 control to use a different UserAgent on the first navigation? |
Unfortunately, by current design, the change to the UserAgent cannot get applied on the first navigation (the same situation on Win32 sample). But we will discuss your suggestion internally. And will let you know if anything changes. Thanks. |
Ok. Thanks for changing it into a feature request as that was indeed my next question. |
As we investigated, "https://www.whatismybrowser.com/" website has a redirection, which will change userAgent back to default. |
Hi @wilva, We have found that, the
We don't know yet if this implementation works as expeceted, and the simple workaround might be using another website to get browser information. |
Hi, Also not sure why a redirect should change the user agent? A user agent is not supposed to be dependent on URL AFAICT. It is certainly possible that the origin of this redirect problem lies deeper than the WebView2 control itself or that there is more than one issue at hand. Either way.. that's some great detective work right there. :) |
Hi @wilva, I am also confused about Later I will communicate with Edge team to get more information and sync with you later. |
Thanks, I don't doubt your findings on this. |
I will close this issue as no fix needed, and feel free to reopen with more info so we can investigate further, thanks. |
Description
Setting the UserAgent is not effective when navigating to a URL.
Only once you navigate to the 2nd URL does the setting apply.
Version
Runtime: Release 106.0.1370.37
Framework: WPF, Win32
OS: Win10
Repro Steps
Take the standard demo WebView2Samples, WebView2WpfBrowser
In MainWindow.xaml change "www.bing.com" into "whatismybrowser.com" like so:
In MainWindowXaml.cs look for "WebView_CoreWebView2InitializationCompleted" and add a line for changing the UserAgent on WebView2 initialization completion.
eg.
Now compile and run.
On first navigation the website will tell you that you are using Edge 106 browser and you're up-to-date.
Press the "Go" button.
And... now the website tells you that it can't find what browser you are using and that the user agent string is "foobar".
It is expected that the user agent string was set on the first time and not after refreshing the site.
The text was updated successfully, but these errors were encountered: