@@ -336,7 +336,7 @@ private async Task<bool> UseDefaultAccountAsync(string userName)
336336 }
337337 }
338338
339- var viewModel = new DefaultAccountViewModel ( Context . Environment )
339+ var viewModel = new DefaultAccountViewModel ( Context . SessionManager )
340340 {
341341 UserName = userName
342342 } ;
@@ -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