如何使用 Microsoft WebView2 同时登录多个帐户

问题描述

我创建了一个带有 webview2 控件的应用程序并运行它并使用用户 A 登录到我的网站,然后再次运行我的应用程序而不关闭一个实例并在同一网站上使用用户 B 登录,然后登录成功.当我检查前一个实例时,用户 A 仍然登录。如何在一个实例应用程序上以相同的方式执行?我用两个 webview2 控件尝试过,当我用用户 B 登录时,用户 A 被注销或会话被用户 B 替换。

解决方法

WebVeiw2 使用 user data folder 来存储 cookie。

要在不同的会话中启动控件的另一个实例,作为一个选项,您可以设置 UserDataFolder,例如:

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");
}

对于那些想要开始使用 WebView2 的人,您需要在您的机器上安装 WebView2 RuntimeMicrosoft Edge Chromium。您还需要在您的项目中安装 WebView2 NuGet package。然后你可以从这里开始:Getting started with WebView2 in Windows Forms