MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
454 stars 55 forks source link

[Problem/Bug]: WebView2 Multiple Instances and userdatafolders #4870

Open DotNet-Fan opened 1 month ago

DotNet-Fan commented 1 month ago

What happened?

Microsoft WebView2 experts,

We have three instances of Microsoft.UI.Xaml.Controls webview2 control where user can enter different credentials for the same webapp with the same url in the 3 instances of webview2.

User has the option to use the same credentials or different credentials for the webapp. How to set different custom Userdatafolder location for these three instances so that multiplelogins can be achieved or switch back to shared userdatafolder?

Tried setting System.Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder.Path) before the call to EnsureCoreWebView2Async() , but somehow the behavior is not consistent with the shared and non shared userdatapath for the webview2. When the app starts , the default is shared userdata location and when user enters the credentials and then set back to multiple different userdatafolder and restart the app and when navigates it doesn't ask for the credentials to enter , but the old credentails is being used for all instances.

Please advise.

Importance

Important. My app's user experience is significantly compromised.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

119.0.2151.97

Framework

WinUI2/UWP

Operating System

Windows 11

OS Version

Microsoft Windows 11 [Version 10.0.22631.4317]

Repro steps

Try setting System.Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder.Path) for one instance and different paths for multiple instances before the call to EnsureCoreWebView2Async()

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

Don't know

Last working version (if regression)

No response

LiangTheDev commented 1 month ago

If the credential is entered in the WebView2 with shared userdata location, there could be some cookies/tokens saved in that WebView2 and upon app restart, those saved credential will be used and therefore no prompt. Even if we login in different WebView2 and then get the token back to the shared WebView2 to use, the credential could still be cached. If we want to always prompt user after app restart, we could call ClearBrowsingData to clear the cached cookies. Another note, CoreWebView2 supports creating WebView2Controller with different profile in a user data folder. That would consume less resources than using a different user data folder. Not sure whether WinUI2 supports it or not though.

DotNet-Fan commented 1 month ago

@LiangTheDev One userdata location for all webview2 instances work , but how to have seperate userdata location for each webview2 instances? That is not working at all if we set multiple paths with System.Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", userDataFolder.Path) for each webview2 instances

LiangTheDev commented 1 month ago

For WinUI2, I am not sure whether there is any good way to do it. One thing you could try is to set the environment variable before triggering the WebView2 initialization for each WebView, you would have to create them one by one.

For WinUI3, you should be able to use EnsureCoreWebView2Async(CoreWebView2Environment, CoreWebView2ControllerOptions) to control the options for the WebVIew2 control.

DotNet-Fan commented 1 month ago

@LiangTheDev
For WinUI2, I am not sure whether there is any good way to do it. One thing you could try is to set the environment variable before triggering the WebView2 initialization for each WebView, you would have to create them one by one.

We are using WinUI2 UWP . The webview2 control is declared in the xaml and before calling the EnsureCoreWebView2Async, tried setting the environment variable System.Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", ) but that is not working as expected and multiple userfolders are not assigned.

LiangTheDev commented 1 month ago

If the WebView2 control has Source property set, it will automatically trigger WebView2 initialization and then it is hard to control the timing between WebView2 initialization and environment variable setting. Is that the case? Have you tried to create the webview2 controls in code for which we would have better control of WebView2 initialization timing.