44namespace Microsoft . Store . PartnerCenter . PowerShell . Authenticators
55{
66 using System ;
7+ using System . Collections . Generic ;
78 using System . Collections . Specialized ;
89 using System . Net ;
910 using System . Net . Sockets ;
@@ -34,9 +35,13 @@ public override async Task<AuthenticationResult> AuthenticateAsync(Authenticatio
3435 AuthenticationResult authResult ;
3536 IClientApplicationBase app ;
3637 InteractiveParameters interactiveParameters = parameters as InteractiveParameters ;
38+ Task < Task < AuthenticationResult > > task ;
3739 TcpListener listener = null ;
40+ int count = 0 ;
3841 string redirectUri = null ;
3942
43+ Queue < string > messages ;
44+
4045 int port = 8399 ;
4146
4247 while ( ++ port < 9000 )
@@ -51,36 +56,62 @@ public override async Task<AuthenticationResult> AuthenticateAsync(Authenticatio
5156 }
5257 catch ( Exception ex )
5358 {
54- Console . WriteLine ( $ "Port { port } is taken with exception '{ ex . Message } '; trying to connect to the next port.") ;
59+ promptAction ( $ "Port { port } is taken with exception '{ ex . Message } '; trying to connect to the next port.") ;
5560 listener ? . Stop ( ) ;
5661 }
5762 }
5863
5964 app = GetClient ( parameters . Account , parameters . Environment , redirectUri ) ;
65+ messages = new Queue < string > ( ) ;
6066
6167 if ( app is IConfidentialClientApplication )
6268 {
63- ICustomWebUi customWebUi = new DefaultOsBrowserWebUi ( interactiveParameters . Message ) ;
69+ ICustomWebUi customWebUi = new DefaultOsBrowserWebUi ( messages , interactiveParameters . Message ) ;
6470
65- Uri authCodeUrl = await customWebUi . AcquireAuthorizationCodeAsync (
66- await app . AsConfidentialClient ( ) . GetAuthorizationRequestUrl ( parameters . Scopes ) . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ,
67- new Uri ( redirectUri ) ,
68- cancellationToken ) . ConfigureAwait ( false ) ;
71+ task = Task < Task < AuthenticationResult > > . Factory . StartNew ( async ( ) =>
72+ {
73+ Uri authCodeUrl = await customWebUi . AcquireAuthorizationCodeAsync (
74+ await app . AsConfidentialClient ( ) . GetAuthorizationRequestUrl ( parameters . Scopes ) . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ,
75+ new Uri ( redirectUri ) ,
76+ cancellationToken ) . ConfigureAwait ( false ) ;
6977
70- NameValueCollection queryStringParameters = HttpUtility . ParseQueryString ( authCodeUrl . Query ) ;
78+ NameValueCollection queryStringParameters = HttpUtility . ParseQueryString ( authCodeUrl . Query ) ;
7179
72- authResult = await app . AsConfidentialClient ( ) . AcquireTokenByAuthorizationCode (
73- parameters . Scopes ,
74- queryStringParameters [ "code" ] ) . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
80+ return await app . AsConfidentialClient ( ) . AcquireTokenByAuthorizationCode (
81+ parameters . Scopes ,
82+ queryStringParameters [ "code" ] ) . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
83+ } ) ;
7584 }
7685 else
7786 {
78- authResult = await app . AsPublicClient ( ) . AcquireTokenInteractive ( parameters . Scopes )
79- . WithCustomWebUi ( new DefaultOsBrowserWebUi ( interactiveParameters . Message ) )
80- . WithPrompt ( Prompt . ForceLogin )
81- . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
87+ task = Task < Task < AuthenticationResult > > . Factory . StartNew ( async ( ) =>
88+ {
89+ return await app . AsPublicClient ( ) . AcquireTokenInteractive ( parameters . Scopes )
90+ . WithCustomWebUi ( new DefaultOsBrowserWebUi ( messages , interactiveParameters . Message ) )
91+ . WithPrompt ( Prompt . ForceLogin )
92+ . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
93+ } ) ;
94+ }
95+
96+ while ( true )
97+ {
98+ while ( messages . Count > 0 )
99+ {
100+ promptAction ( messages . Dequeue ( ) ) ;
101+ count ++ ;
102+ }
103+
104+ if ( count >= 2 )
105+ {
106+ break ;
107+ }
108+
109+ cancellationToken . ThrowIfCancellationRequested ( ) ;
110+ Thread . Sleep ( 1000 ) ;
82111 }
83112
113+ authResult = await task . Result . ConfigureAwait ( false ) ;
114+
84115 return authResult ;
85116 }
86117
0 commit comments