11
11
using System . Text ;
12
12
using System . Threading ;
13
13
using GitCredentialManager . UI ;
14
+ using GitCredentialManager . UI . Controls ;
14
15
using GitCredentialManager . UI . ViewModels ;
15
16
using GitCredentialManager . UI . Views ;
16
17
using Microsoft . Identity . Client . AppConfig ;
17
18
18
19
#if NETFRAMEWORK
19
- using System . Drawing ;
20
- using System . Windows . Forms ;
21
20
using Microsoft . Identity . Client . Broker ;
22
21
#endif
23
22
@@ -118,10 +117,6 @@ public class MicrosoftAuthentication : AuthenticationBase, IMicrosoftAuthenticat
118
117
"live" , "liveconnect" , "liveid" ,
119
118
} ;
120
119
121
- #if NETFRAMEWORK
122
- private DummyWindow _dummyWindow ;
123
- #endif
124
-
125
120
public MicrosoftAuthentication ( ICommandContext context )
126
121
: base ( context ) { }
127
122
@@ -130,6 +125,8 @@ public MicrosoftAuthentication(ICommandContext context)
130
125
public async Task < IMicrosoftAuthenticationResult > GetTokenForUserAsync (
131
126
string authority , string clientId , Uri redirectUri , string [ ] scopes , string userName , bool msaPt )
132
127
{
128
+ var uiCts = new CancellationTokenSource ( ) ;
129
+
133
130
// Check if we can and should use OS broker authentication
134
131
bool useBroker = CanUseBroker ( ) ;
135
132
Context . Trace . WriteLine ( useBroker
@@ -144,7 +141,7 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(
144
141
try
145
142
{
146
143
// 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 ) ;
148
145
149
146
AuthenticationResult result = null ;
150
147
@@ -261,10 +258,8 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(
261
258
}
262
259
finally
263
260
{
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 ( ) ;
268
263
}
269
264
}
270
265
@@ -451,8 +446,8 @@ private async Task<AuthenticationResult> GetAccessTokenSilentlyAsync(
451
446
}
452
447
}
453
448
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 )
456
451
{
457
452
var httpFactoryAdaptor = new MsalHttpClientFactoryAdaptor ( Context . HttpClientFactory ) ;
458
453
@@ -495,11 +490,8 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
495
490
}
496
491
else if ( enableBroker ) // Only actually need to set a parent window when using the Windows broker
497
492
{
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 ) ) ;
503
495
}
504
496
}
505
497
}
@@ -899,73 +891,5 @@ public MsalResult(AuthenticationResult msalResult)
899
891
public string AccessToken => _msalResult . AccessToken ;
900
892
public string AccountUpn => _msalResult . Account ? . Username ;
901
893
}
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
970
894
}
971
895
}
0 commit comments