Skip to content
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

Closed
wilva opened this issue Oct 10, 2022 · 19 comments
Closed

Setting UserAgent not effective on first navigation. #2862

wilva opened this issue Oct 10, 2022 · 19 comments
Assignees
Labels
bug Something isn't working

Comments

@wilva
Copy link

wilva commented Oct 10, 2022

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:

            <wv2:WebView2
                x:Name="webView"
                CreationProperties="{StaticResource EvergreenWebView2CreationProperties}"
                Source="https://www.whatismybrowser.com/"
            />

In MainWindowXaml.cs look for "WebView_CoreWebView2InitializationCompleted" and add a line for changing the UserAgent on WebView2 initialization completion.
eg.


        void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            if (e.IsSuccess)
            {
                webView.CoreWebView2.ProcessFailed += WebView_ProcessFailed;
                webView.CoreWebView2.DocumentTitleChanged += WebView_DocumentTitleChanged;
                webView.CoreWebView2.IsDocumentPlayingAudioChanged += WebView_IsDocumentPlayingAudioChanged;
                webView.CoreWebView2.IsMutedChanged += WebView_IsMutedChanged;
                WebViewSettings.UserAgent = "foobar";

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.

@wilva wilva added the bug Something isn't working label Oct 10, 2022
@victorthoang
Copy link

hello @wilva,

Thanks for your bug report. We will evaluate this bug and get back to you with our findings. @imicy for further investigation and triaging.

@imicy imicy assigned ElyssaJyu and unassigned imicy Oct 12, 2022
@ElyssaJyu
Copy link

ElyssaJyu commented Oct 12, 2022

Hi @wilva,
since setting change to UserAgent is made after NavigationStarting event, it won't be applied until the next top-level navigation, as documented in CoreWebView2Settings.

@wilva
Copy link
Author

wilva commented Oct 12, 2022

Hello @ElyssaJyu,

That makes sense, thank you.
The problem is that it still does not appear to work well. I have tested it a number of times and sometimes it appears to work like that and -at least for me- most of the time it does not.

To stay with the example from above. First set the .Source in MainWindow.xaml above to an empty string.
eg.

            <wv2:WebView2
                x:Name="webView"
                CreationProperties="{StaticResource EvergreenWebView2CreationProperties}"
                Source=""
            />

So now no site is loaded on starting the demo application.

On running the example again, it does not instantiate the WebView2 control.
Go to the address bar and enter "https://www.whatismybrowser.com/" in the URL bar, then click on the "Go" button.

It usually comes back with "Edge 106" as useragent, but also every now and then with "FooBar".
FWIW I have changed the URL a bit sometimes as "https://whatismybrowser.com/" (without the www. part), sometimes without the backslash at the end "https://www.whatismybrowser.com". Haven't found much consistency in that.

When looking at the events in a debugger it first comes past WebView_CoreWebView2InitializationCompleted and after that it triggers WebView_NavigationStarting.
So that would suggest that the creation of the WebView2 control is complete and the UserAgent is set before NavigationStarting is triggered. Yet the website still reports the wrong user agent.

@wilva
Copy link
Author

wilva commented Oct 12, 2022

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.

@ElyssaJyu
Copy link

ElyssaJyu commented Oct 13, 2022

Hi @wilva,
As you mentioned, whether you try to leave Source as blank then enter website in the URL bar or set Source="https://www.whatismybrowser.com/" , there will be the first time to trigger NavigationStarting event, then set the property change to the server, but it won't be valid until next NavigationStarting event. The same as Win32 interface. Refer to interface ICoreWebView2Settings

@wilva
Copy link
Author

wilva commented Oct 13, 2022

Hi @ElyssaJyu,

OK, let's remove that debate about what was first triggered.

Make the following additional edit:
from

        void IncreaseZoomCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
                        webView.ZoomFactor += ZoomStep();
        }

into

        async void IncreaseZoomCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            await webView.EnsureCoreWebView2Async();
        }

In addition, to remove any doubt, comment out the creation of the async creation of the WebView2 control from the "go" button logic:


        async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
//            await webView.EnsureCoreWebView2Async();


Comment out the first line down there.

Compile and run the program. Select "Increase Zoom" from the menu.
The WebView2 control is created and shows a nice "about:blank" in the URL bar.
It also already came past the WebView_CoreWebView2InitializationCompleted event and set the UserAgent to "FooBar".
So right now we're SURE that the property was changed.

Now add "https://www.whatismybrowser.com" in the URL bar, press "Go".
It still displays "Edge 106" as the User Agent and only after I press the "Go" button another time, it changes into "FooBar".

@ElyssaJyu
Copy link

ElyssaJyu commented Oct 14, 2022

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.
Please remind "setting changes made after NavigationStarting event do not apply until the next top-level navigation."
image
The Set UserAgent is a change to the local settings object and is not passed to the server. It won't be passed to the server until the next time Navigation starts the event.
After the WebView2 control is created, the change on user agent will send to the server until the NavigationStarting event triggers, but this property will not be applied immediately, and it needs to wait for the next time navigation.
I hope this will clarify your doubt.

@wilva
Copy link
Author

wilva commented Oct 14, 2022

Hi @ElyssaJyu ,

After the WebView2 control is created, the set user agent triggers the NavigationStarting event and passes the property to the server

I do not see setting UserAgent triggering the NavigationStarting event.

Following my steps, that I described above, I see the following happen:

  1. The control gets created by selecting the "Increase Zoom" menu option. Note that I changed the menu option above to create the control instead of applying zoom, it now is a "create and render webview2 control" menu option.
  2. During WebView_CoreWebView2InitializationCompleted event, the UserAgent is set to "FooBar".
  3. The control is rendered, the URL bar shows "about:blank".
  4. There is NO navigationStarting event triggered at this time (which I confirmed by having a breakpoint on it)
  5. Enter "https://www.whatismybrowser.com" in the URL bar and press the "go" button.
  6. Now the NavigationStarting event triggers for the first time. This event triggers two times. It does this regardless of my line of code setting the UserAgent is commented out or not.

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.
Maybe you are trying to say that it is during the NavigationStarting event that the UserAgent setting gets applied? But that the ship has sailed by that time?

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

@wilva
Copy link
Author

wilva commented Oct 14, 2022

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.
Except you need to comment out the Start page logic like so:
from:

        void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            if (e.IsSuccess)
            {
                // Setup host resource mapping for local files
                webView.CoreWebView2.SetVirtualHostNameToFolderMapping("appassets.example", "assets", CoreWebView2HostResourceAccessKind.DenyCors);
                // Set StartPage Uri
                webView.Source = new Uri(GetStartPageUri(webView.CoreWebView2));

into:

        void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            if (e.IsSuccess)
            {
                //// Setup host resource mapping for local files
                //webView.CoreWebView2.SetVirtualHostNameToFolderMapping("appassets.example", "assets", CoreWebView2HostResourceAccessKind.DenyCors);
                //// Set StartPage Uri
                //webView.Source = new Uri(GetStartPageUri(webView.CoreWebView2));
                WebViewSettings.UserAgent = "foobar";

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.

@ElyssaJyu
Copy link

ElyssaJyu commented Oct 17, 2022

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.
Also, I reproduce the demo by the steps.

  • Based on your setting, select the "Increase Zoom" menu option, initialization of the control's CoreWebView2 triggers.
  • During WebView_CoreWebView2InitializationCompleted event, the UserAgent is set to "FooBar". (But not send to the server yet at this moment)
  • Enter "https://www.whatismybrowser.com/" in the URL bar and press the "go" button, then NavigationStarting event triggers (the change of UserAgent sends to the server at this moment, but not get applied immediately)
  • on the next time NavigationStarting event triggers, "FooBar" is reported. (what's why you will see "FooBar" useragent report after press "go" button twice).

@wilva
Copy link
Author

wilva commented Oct 17, 2022

Thank you @ElyssaJyu.

So is it not possible for the WebView2 control to use a different UserAgent on the first navigation?
Am I doing something wrong?

@ElyssaJyu
Copy link

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.

@wilva
Copy link
Author

wilva commented Oct 17, 2022

Ok.

Thanks for changing it into a feature request as that was indeed my next question.

@ElyssaJyu
Copy link

As we investigated, "https://www.whatismybrowser.com/" website has a redirection, which will change userAgent back to default.
image

image

@plantree
Copy link
Contributor

Hi @wilva,

We have found that, the URL redirect will restore the UserAgent. You can test it in Edge too. Like this:

Custom set user-agent in DevTools, and type http://www.whatismybrowser.com/. It will redirect to https://www.whatismybrowser.com/.

We don't know yet if this implementation works as expeceted, and the simple workaround might be using another website to get browser information.

@wilva
Copy link
Author

wilva commented Oct 18, 2022

Hi,
It was my customer who brought this up. I don't think the website redirecting is relevant, but I will ask them.

Also not sure why a redirect should change the user agent? A user agent is not supposed to be dependent on URL AFAICT.
IMHO it should not matter what website you are accessing, after you set the User Agent to something else it should be consistently used for any website that you access, with or without redirects.

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. :)

@plantree
Copy link
Contributor

Hi @wilva,

I am also confused about redirect will reset user-agent, but you could verify on Edge according to the method that Elyssa provides.

Later I will communicate with Edge team to get more information and sync with you later.

@wilva
Copy link
Author

wilva commented Oct 19, 2022

Thanks, I don't doubt your findings on this.

@ElyssaJyu
Copy link

I will close this issue as no fix needed, and feel free to reopen with more info so we can investigate further, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants