1111using System . Text ;
1212using System . Threading ;
1313using GitCredentialManager . UI ;
14+ using GitCredentialManager . UI . Controls ;
1415using GitCredentialManager . UI . ViewModels ;
1516using GitCredentialManager . UI . Views ;
1617using Microsoft . Identity . Client . AppConfig ;
1718
1819#if NETFRAMEWORK
19- using System . Drawing ;
20- using System . Windows . Forms ;
2120using Microsoft . Identity . Client . Broker ;
2221#endif
2322
@@ -118,10 +117,6 @@ public class MicrosoftAuthentication : AuthenticationBase, IMicrosoftAuthenticat
118117 "live" , "liveconnect" , "liveid" ,
119118 } ;
120119
121- #if NETFRAMEWORK
122- private DummyWindow _dummyWindow ;
123- #endif
124-
125120 public MicrosoftAuthentication ( ICommandContext context )
126121 : base ( context ) { }
127122
@@ -130,6 +125,8 @@ public MicrosoftAuthentication(ICommandContext context)
130125 public async Task < IMicrosoftAuthenticationResult > GetTokenForUserAsync (
131126 string authority , string clientId , Uri redirectUri , string [ ] scopes , string userName , bool msaPt )
132127 {
128+ var uiCts = new CancellationTokenSource ( ) ;
129+
133130 // Check if we can and should use OS broker authentication
134131 bool useBroker = CanUseBroker ( ) ;
135132 Context . Trace . WriteLine ( useBroker
@@ -144,7 +141,7 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(
144141 try
145142 {
146143 // Create the public client application for authentication
147- IPublicClientApplication app = await CreatePublicClientApplicationAsync ( authority , clientId , redirectUri , useBroker , msaPt ) ;
144+ IPublicClientApplication app = await CreatePublicClientApplicationAsync ( authority , clientId , redirectUri , useBroker , msaPt , uiCts ) ;
148145
149146 AuthenticationResult result = null ;
150147
@@ -261,10 +258,8 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(
261258 }
262259 finally
263260 {
264- #if NETFRAMEWORK
265- // If we created a dummy window during authentication we should dispose of it now that we're done
266- _dummyWindow ? . Dispose ( ) ;
267- #endif
261+ // If we created some global UI (e.g. progress) during authentication we should dismiss them now that we're done
262+ uiCts . Cancel ( ) ;
268263 }
269264 }
270265
@@ -451,8 +446,8 @@ private async Task<AuthenticationResult> GetAccessTokenSilentlyAsync(
451446 }
452447 }
453448
454- private async Task < IPublicClientApplication > CreatePublicClientApplicationAsync (
455- string authority , string clientId , Uri redirectUri , bool enableBroker , bool msaPt )
449+ private async Task < IPublicClientApplication > CreatePublicClientApplicationAsync ( string authority ,
450+ string clientId , Uri redirectUri , bool enableBroker , bool msaPt , CancellationTokenSource uiCts )
456451 {
457452 var httpFactoryAdaptor = new MsalHttpClientFactoryAdaptor ( Context . HttpClientFactory ) ;
458453
@@ -495,11 +490,8 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
495490 }
496491 else if ( enableBroker ) // Only actually need to set a parent window when using the Windows broker
497492 {
498- #if NETFRAMEWORK
499- Context . Trace . WriteLine ( $ "Using dummy parent window for MSAL authentication dialogs.") ;
500- _dummyWindow = new DummyWindow ( ) ;
501- appBuilder . WithParentActivityOrWindow ( _dummyWindow . ShowAndGetHandle ) ;
502- #endif
493+ Context . Trace . WriteLine ( "Using progress parent window for MSAL authentication dialogs." ) ;
494+ appBuilder . WithParentActivityOrWindow ( ( ) => ProgressWindow . ShowAndGetHandle ( uiCts . Token ) ) ;
503495 }
504496 }
505497 }
@@ -899,73 +891,5 @@ public MsalResult(AuthenticationResult msalResult)
899891 public string AccessToken => _msalResult . AccessToken ;
900892 public string AccountUpn => _msalResult . Account ? . Username ;
901893 }
902-
903- #if NETFRAMEWORK
904- private class DummyWindow : IDisposable
905- {
906- private readonly Thread _staThread ;
907- private readonly ManualResetEventSlim _readyEvent ;
908- private Form _window ;
909- private IntPtr _handle ;
910-
911- public DummyWindow ( )
912- {
913- _staThread = new Thread ( ThreadProc ) ;
914- _staThread . SetApartmentState ( ApartmentState . STA ) ;
915- _readyEvent = new ManualResetEventSlim ( ) ;
916- }
917-
918- public IntPtr ShowAndGetHandle ( )
919- {
920- _staThread . Start ( ) ;
921- _readyEvent . Wait ( ) ;
922- return _handle ;
923- }
924-
925- public void Dispose ( )
926- {
927- _window ? . Invoke ( ( ) => _window . Close ( ) ) ;
928-
929- if ( _staThread . IsAlive )
930- {
931- _staThread . Join ( ) ;
932- }
933- }
934-
935- private void ThreadProc ( )
936- {
937- System . Windows . Forms . Application . EnableVisualStyles ( ) ;
938- _window = new Form
939- {
940- TopMost = true ,
941- ControlBox = false ,
942- MaximizeBox = false ,
943- MinimizeBox = false ,
944- ClientSize = new Size ( 182 , 46 ) ,
945- FormBorderStyle = FormBorderStyle . None ,
946- StartPosition = FormStartPosition . CenterScreen ,
947- } ;
948-
949- var progress = new ProgressBar
950- {
951- Style = ProgressBarStyle . Marquee ,
952- Location = new Point ( 12 , 12 ) ,
953- Size = new Size ( 158 , 23 ) ,
954- MarqueeAnimationSpeed = 30 ,
955- } ;
956-
957- _window . Controls . Add ( progress ) ;
958- _window . Shown += ( s , e ) =>
959- {
960- _handle = _window . Handle ;
961- _readyEvent . Set ( ) ;
962- } ;
963-
964- _window . ShowDialog ( ) ;
965- _window . Dispose ( ) ;
966- _window = null ;
967- }
968- }
969- #endif
970894 }
971895}
0 commit comments