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

RuntimeBest forRisk
EvergreenMost appsUser machines receive runtime updates automatically
Fixed VersionOffline, compatibility-locked, regulated environmentsYou must ship and update browser bits yourself

Evergreen is the safer default because users get security updates through the WebView2 runtime/Edge release channel.

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.