Skip to content

WebView2 UserData Demo - Multi-Account Session Isolation ​

Overview ​

Demonstrates cWebView2Host's multi-instance session isolation functionality. In an MDI application, each child form has an independent WebView2 instance and UserDataFolder, enabling multi-account parallel login with independent cookies and localStorage.

Project Structure ​

Download Source [ Note: Re-register the DLL file in the bin directory ]

Source code is available in the VBMAN2 project's demos/webview2/userData directory.

userData/
  ├── MDIForm1.frm           # MDI parent form, managing child form creation
  ├── Form1.frm              # MDI child form, containing WebView2 instance
  ├── pic/                   # Screenshots of running result
  │   ├── 001.png
  │   ├── 002.png
  │   ├── 003.png
  │   ├── 004.png
  │   └── 005.png
  ├── UserDir/               # Pre-created user data directories
  │   ├── account-default/EBWebView/
  │   ├── account-001/EBWebView/
  │   ├── account-002/EBWebView/
  │   └── account-011/EBWebView/
  ├── vbman2_webview2.vbp    # VB6 project file (startup object = MDIForm1)
  └── vbman2_webview2.vbw    # VB6 workspace file

Core Code Walkthrough ​

1. MDI Parent Form (MDIForm1.frm) ​

vb
Private Sub MDIForm_Load()
    Form1.Init    ' Open default child window
End Sub

Private Sub OpenNewPage_Click()
    Dim Data As String: Data = InputBox(Tip, , "001,http://vb6.pro")
    If Data = "" Then Exit Sub
    Dim AppFix As String: AppFix = Left(Data, 4)
    Dim f As New Form1
    f.Init Left$(AppFix, 3), Mid$(Data, 5)   ' Account suffix + URL
End Sub

Uses Dim f As New Form1 to dynamically create MDI child form instances; each input format is account_suffix,URL.

2. MDI Child Form (Form1.frm) ​

vb
Dim WithEvents wv As cWebView2Host
Dim AccountAppendFix As String
Dim ThisUrl As String

Public Sub Init(Optional ByVal UserDataFix As String, Optional ByVal Url As String)
    Me.Show
    If Url = "" Then Url = BASE_URL
    ThisUrl = Url
    AccountAppendFix = UserDataFix
    If AccountAppendFix = "" Then AccountAppendFix = "default"
    wv.Initialize Me    ' Pass Form object
End Sub

3. Three-Phase Initialization - Configure UserDataFolder ​

vb
Private Sub wv_Create()
    ' Set independent user data directory
    wv.EnvironmentOptions.UserDataFolder = App.Path & "\UserDir\account-" & AccountAppendFix
End Sub

Private Sub wv_Ready()
    ' Navigate to target URL
    wv.Navigate ThisUrl
End Sub

4. Title Synchronization ​

vb
Private Sub wv_DocumentTitleChanged()
    Me.Caption = wv.DocumentTitle
End Sub

Feature Description ​

  1. Multi-Account Session Isolation

    • Each child form's WebView2 uses a different UserDataFolder
    • Cookies, localStorage, and login state are completely independent
    • The same website can be logged in with different accounts in different child forms
  2. Three-Phase Initialization Pattern

    • Initialize Me → Create WebView2 (don't pass URL)
    • wv_Create() → Configure UserDataFolder
    • wv_Ready() → Execute Navigate
  3. MDI Multi-Instance Architecture

    • MDIForm1 as parent form
    • Each child form independently creates cWebView2Host instance
    • Dynamic creation/destruction of child forms
  4. Pre-Created Data Directories

    • UserDir/account-default/ and similar directories are pre-created
    • WebView2 creates EBWebView subdirectory within the directory
    • Contains complete browser data (Cache, Cookies, Local Storage, etc.)

Technical Notes ​

  1. UserDataFolder Purpose: WebView2 stores all user data (Cookies, Cache, localStorage, Session Storage, IndexedDB, etc.) in this directory. Different directories = completely isolated browser sessions

  2. wv.Initialize Me: Passes the Form object; internally auto-gets hWnd. Equivalent to wv.Initialize Me.hWnd

  3. wv_Create vs wv_Ready:

    • Create fires immediately after WebView2 control creation — the best time to configure environment options
    • Ready fires after the WebView2 control initialization completes
  4. Multi-Instance Note: WebView2 instances sharing the same UserDataFolder share the same browser process; different UserDataFolders create independent browser processes

Use Cases ​

  1. Multi-account management systems (social media, e-commerce platforms)
  2. Parallel testing of web applications with different user roles
  3. Browser-isolated private browsing
  4. Multi-tenant SaaS application clients

Extension Suggestions ​

  1. Add account switching functionality in child forms by switching UserDataFolder
  2. Record each account's UserDataFolder path for account persistence
  3. Add cookie export/import functionality for account migration

VB6 and LOGO copyright of Microsoft Corporation