편집

다음을 통해 공유


Support multiple profiles under a single user data folder

The WebView2 Multiple Profiles API allows you to create and manipulate user profiles to work with your WebView2 controls. Profiles in WebView2 are conceptually similar to profiles in Microsoft Edge. Multiple profile support enables a WebView2 app to have multiple profiles under a single user data folder.

Each profile has a dedicated profile folder to save browser data, which provides separate browsing data storage for each user, such as cookies, user preference settings, and cached resources. All the WebView2 controls that are associated with the same user profile share a single profile folder.

Previous approach: Using a different user data folder for each WebView2 control

Previously, without multi-profile support, to achieve data separation, a WebView2 app could use different user data folders for different WebView2 controls. However, in that approach, you must run multiple WebView2 runtime instances (each including a browser process and a bunch of child processes), which consumed more system resources including memory, CPU footprint, and disk space.

Specify the profile when creating a WebView2

Create an options object that defines a profile

The CreateCoreWebView2ControllerOptions method on CoreWebView2Environment creates an options object, CoreWebView2ControllerOptions, to provide specific information about a profile, including ProfileName and IsInPrivateModeEnabled. Use this object to specify the target profile when creating a WebView2 control instance.

Create a WebView2 control that uses the profile

Each Create...Controller method which takes an options parameter creates a WebView2 control and associates it with the profile you specified. If the specified profile doesn't exist, a new profile will be created.

Example of specifying the profile when creating a WebView2

public async void CreateWebView2Controller(object sender, RoutedEventArgs e)
{
    var hwnd = new HwndForWV2();
    Window window = new Window();
    window.Content = hwnd;
    window.Show();

    var env = await CoreWebView2Environment.CreateAsync();
    var options = env.CreateCoreWebView2ControllerOptions();
    options.ProfileName = "MyProfile";
    options.IsInPrivateModeEnabled = true;
    var controller = await env.CreateCoreWebView2ControllerAsync(hwnd.Handle, options);

    controller.Bounds = new System.Drawing.Rectangle(0, 0, 800, 600);
    controller.CoreWebView2.Navigate("https://www.bing.com/");
    controller.CoreWebView2.NavigationStarting += CoreWebView_NavigationStarting;
}

Access and manipulate the profile

You can get the profile object by accessing the Profile property of a WebView2 control.

After you get the profile object, you can manipulate it. Use CoreWebView2Profile to get profile information and do profile-wide settings and operations.

Example of accessing and manipulating the profile

string profileName = controller.CoreWebView2.Profile.ProfileName;
bool inPrivate = controller.CoreWebView2.Profile.IsInPrivateModeEnabled;

// update window title with profileName
UpdateAppTitle(profileName);

// update window icon
SetAppIcon(inPrivate);

See also