Skip to content

Commit 88522e9

Browse files
authored
Override MSAL's system webview handling (#2258)
Since we have special handling on Linux (eg WSL support), we try to use our own logic for launching the user's default web browser for interactive authentication. In the event we fail to start the browser, we fall back to using MSAL's logic, with a preference of Edge (Chromium-based) - largely because this is the only way to hook ourselves back into MSAL's default handling.
2 parents 1d07064 + 77f109c commit 88522e9

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/shared/Core/Authentication/MicrosoftAuthentication.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -754,10 +754,33 @@ private static EmbeddedWebViewOptions GetEmbeddedWebViewOptions()
754754
};
755755
}
756756

757-
private static SystemWebViewOptions GetSystemWebViewOptions()
757+
private SystemWebViewOptions GetSystemWebViewOptions()
758758
{
759759
// TODO: add nicer HTML success and error pages
760-
return new SystemWebViewOptions();
760+
return new SystemWebViewOptions
761+
{
762+
OpenBrowserAsync = OpenBrowserFunc
763+
};
764+
765+
// We have special handling for Linux and WSL to open the system browser
766+
// so we need to use our own function here. Sorry MSAL!
767+
Task OpenBrowserFunc(Uri uri)
768+
{
769+
try
770+
{
771+
Context.SessionManager.OpenBrowser(uri);
772+
}
773+
catch (Exception ex)
774+
{
775+
Context.Trace.WriteLine("Failed to open system web browser - using MSAL fallback");
776+
Context.Trace.WriteException(ex);
777+
778+
// Fallback to MSAL's default browser opening logic, preferring Edge.
779+
return SystemWebViewOptions.OpenWithChromeEdgeBrowserAsync(uri);
780+
}
781+
782+
return Task.CompletedTask;
783+
}
761784
}
762785

763786
private Task ShowDeviceCodeInTty(DeviceCodeResult dcr)
@@ -859,8 +882,14 @@ private void EnsureCanUseEmbeddedWebView()
859882

860883
private bool CanUseSystemWebView(IPublicClientApplication app, Uri redirectUri)
861884
{
885+
//
862886
// MSAL requires the application redirect URI is a loopback address to use the System WebView
863-
return Context.SessionManager.IsWebBrowserAvailable && app.IsSystemWebViewAvailable && redirectUri.IsLoopback;
887+
//
888+
// Note: we do NOT check the MSAL 'IsSystemWebViewAvailable' property as it only
889+
// looks for the presence of the DISPLAY environment variable on UNIX systems.
890+
// This is insufficient as we instead handle launching the default browser ourselves.
891+
//
892+
return Context.SessionManager.IsWebBrowserAvailable && redirectUri.IsLoopback;
864893
}
865894

866895
private void EnsureCanUseSystemWebView(IPublicClientApplication app, Uri redirectUri)
@@ -871,12 +900,6 @@ private void EnsureCanUseSystemWebView(IPublicClientApplication app, Uri redirec
871900
"System web view is not available without a way to start a browser.");
872901
}
873902

874-
if (!app.IsSystemWebViewAvailable)
875-
{
876-
throw new Trace2InvalidOperationException(Context.Trace2,
877-
"System web view is not available on this platform.");
878-
}
879-
880903
if (!redirectUri.IsLoopback)
881904
{
882905
throw new Trace2InvalidOperationException(Context.Trace2,

0 commit comments

Comments
 (0)