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 fileCore Code Walkthrough
1. MDI Parent Form (MDIForm1.frm)
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 SubUses 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)
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 Sub3. Three-Phase Initialization - Configure UserDataFolder
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 Sub4. Title Synchronization
Private Sub wv_DocumentTitleChanged()
Me.Caption = wv.DocumentTitle
End SubFeature Description
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
Three-Phase Initialization Pattern
Initialize Me→ Create WebView2 (don't pass URL)wv_Create()→ Configure UserDataFolderwv_Ready()→ Execute Navigate
MDI Multi-Instance Architecture
- MDIForm1 as parent form
- Each child form independently creates cWebView2Host instance
- Dynamic creation/destruction of child forms
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
UserDataFolder Purpose: WebView2 stores all user data (Cookies, Cache, localStorage, Session Storage, IndexedDB, etc.) in this directory. Different directories = completely isolated browser sessions
wv.Initialize Me: Passes the Form object; internally auto-gets hWnd. Equivalent to
wv.Initialize Me.hWndwv_Create vs wv_Ready:
Createfires immediately after WebView2 control creation — the best time to configure environment optionsReadyfires after the first navigation completes
Multi-Instance Note: WebView2 instances sharing the same UserDataFolder share the same browser process; different UserDataFolders create independent browser processes
Use Cases
- Multi-account management systems (social media, e-commerce platforms)
- Parallel testing of web applications with different user roles
- Browser-isolated private browsing
- Multi-tenant SaaS application clients
Extension Suggestions
- Add account switching functionality in child forms by switching UserDataFolder
- Record each account's UserDataFolder path for account persistence
- Add cookie export/import functionality for account migration