WebView2 Runtime And Security
WebView2 adds a browser engine to a native app. Treat it as a runtime dependency and a security boundary.
Runtime Choice
| Runtime | Best for | Risk |
|---|---|---|
| Evergreen | Most apps | User machines receive runtime updates automatically |
| Fixed Version | Offline, compatibility-locked, regulated environments | You must ship and update browser bits yourself |
Evergreen is the safer default because users get security updates through the WebView2 runtime/Edge release channel.
Navigation Policy
await Web.EnsureCoreWebView2Async();
Web.CoreWebView2.NavigationStarting += (_, args) =>
{
var uri = new Uri(args.Uri);
args.Cancel = uri.Scheme != Uri.UriSchemeHttps || uri.Host != "app.example.com";
};
Host Object Discipline
Host object injection can expose native capabilities to web content. Keep the exposed API tiny, origin-gated, and testable.
Do not expose broad filesystem, shell, credential, or process APIs to arbitrary pages.
User Data Folder
Decide where cookies, cache, and profiles live. The choice affects privacy, account switching, uninstall behavior, and test cleanup.
Testing
- Smoke native launch and WebView2 initialization.
- Test blocked navigation.
- Test offline/error UI.
- Use Playwright for complex web flows.
- Test fixed runtime path if you choose fixed runtime.