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

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

Open
DotNet-Fan opened this issue Oct 21, 2024 · 5 comments
Open

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

DotNet-Fan opened this issue Oct 21, 2024 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@DotNet-Fan
Copy link

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

@DotNet-Fan DotNet-Fan added the bug Something isn't working label Oct 21, 2024
@LiangTheDev
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

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
Copy link
Author

DotNet-Fan commented Oct 24, 2024

@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
Copy link
Member

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.

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

2 participants