Skip to content

WebView2

pmaytak edited this page May 21, 2021 · 15 revisions

What is WebView2

A modern embedded browser based on Microsoft Edge, capable of performing Windows Hello, log-in with FIDO keys, etc. This browser replaces the old embedded WebView, based on an outdated version of Internet Explorer.

Where is it available?

  • All Windows versions
  • MSAL version 4.28.0 and higher
  • WebView2 runtime must be installed on the machine

Evergreen runtime

WebView2 runtime is not available on Windows by default. Applications should install it or guide the end users to install it. Installing it requires admin access.

Fixed runtime

As an alternative to the evergreen runtime, apps can bundle a fixed runtime.

var builder = pca.AcquireTokenInteractive(scopes)
  .WithUseEmbeddedWebView(true);

// checks for the evergreen WebView2 runtime 
if (!pca.IsEmbeddedWebViewAvailable())
{
    builder = builder.WithEmbeddedWebViewOptions(
        new EmbeddedWebViewOptions()
        {
            WebView2BrowserExecutableFolder = @"c:\my_app\webview2\fixed_version"
        });
}

Changes to call pattern

var pca = PublicClienntApplicationBuilder
    .Create("client_id")
    .WithDesktopFeatures()
    .Build()

Behaviour

Embedded WebView Default WebView
.NET Fx WebView2, fallback to Legacy Embedded
.NET Core WebView2, fallback to Legacy* Embedded
.NET 5 WebView2, fallback to Legacy* Embedded

*In .NET Core and .NET 5, fallback to legacy WebView is available starting in MSAL 4.30.0.

Troubleshooting

Apps that target .NET Framework and try to open a WebView2 embedded browser can get System.BadImageFormatException: An attempt was made to load a program with an incorrect format. or System.DllNotFoundException: 'Unable to load DLL 'WebView2Loader.dll': The specified module could not be found. exceptions. (As of MSAL.NET 4.32.0 these exceptions are wrapped into MsalClientException.) This occurs because of an existing bug in the WebView2 SDK on .NET Classic platform. One possible workaround is to add a <PlatformTarget> with values AnyCPU, x86, or x64 to your app's project file. (x86 or x64 have to match the target framework of the WebView2 installed on the machine.) For details, see issue #730 in WebView2Feedback repository.

Getting started with MSAL.NET

Acquiring tokens

Desktop/Mobile apps

Web Apps / Web APIs / daemon apps

Advanced topics

News

FAQ

Other resources

Clone this wiki locally