How to login to multiple account at same time with Microsoft WebView2

Viewed 1235

I have create an application with a webview2 control and run it and do login with user A to my website, then run my application again without closing the previous instance and do login with user B on same website, then the login is successful. And when I check the previous instance, the user A is still logged in. How to do the same way but on one instance application? I have tried it with two webview2 control and when I do login with user B, the user A is logged out or the session is replaced with user B.

1 Answers

WebVeiw2 uses user data folder to store the cookies.

To start another instance of the control in a different session, as an option, you can set UserDataFolder, for example:

private async void button1_Click(object sender, EventArgs e)
{
    webView21.CreationProperties =
        new Microsoft.Web.WebView2.WinForms.CoreWebView2CreationProperties();
    webView21.CreationProperties.UserDataFolder = System.IO.Path.Combine(
        Application.StartupPath, $"{Guid.NewGuid()}");
    await webView21.EnsureCoreWebView2Async();
    webView21.CoreWebView2.Navigate("https://outlook.com");
}

For those who want to start using WebView2, you need to have WebView2 Runtime and Microsoft Edge Chromium installed on your machine. You also need to install WebView2 NuGet package in your project. Then you can start here: Getting started with WebView2 in Windows Forms.

Related