@@ -206,36 +206,10 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
206
206
207
207
IPublicClientApplication app = GetPublicClientAppInstance ( pcaKey ) ;
208
208
209
- if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryIntegrated )
210
- {
211
- if ( ! string . IsNullOrEmpty ( parameters . UserId ) )
212
- {
213
- result = await app . AcquireTokenByIntegratedWindowsAuth ( scopes )
214
- . WithCorrelationId ( parameters . ConnectionId )
215
- . WithUsername ( parameters . UserId )
216
- . ExecuteAsync ( cancellationToken : cts . Token )
217
- . ConfigureAwait ( false ) ;
218
- }
219
- else
220
- {
221
- result = await app . AcquireTokenByIntegratedWindowsAuth ( scopes )
222
- . WithCorrelationId ( parameters . ConnectionId )
223
- . ExecuteAsync ( cancellationToken : cts . Token )
224
- . ConfigureAwait ( false ) ;
225
- }
226
- SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token for Active Directory Integrated auth mode. Expiry Time: {0}" , result ? . ExpiresOn ) ;
227
- }
228
- else if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryPassword )
229
- {
230
- result = await app . AcquireTokenByUsernamePassword ( scopes , parameters . UserId , parameters . Password )
231
- . WithCorrelationId ( parameters . ConnectionId )
232
- . ExecuteAsync ( cancellationToken : cts . Token )
233
- . ConfigureAwait ( false ) ;
234
-
235
- SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token for Active Directory Password auth mode. Expiry Time: {0}" , result ? . ExpiresOn ) ;
236
- }
237
- else if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryInteractive ||
238
- parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryDeviceCodeFlow )
209
+ if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryIntegrated ||
210
+ parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryPassword ||
211
+ parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryInteractive ||
212
+ parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryDeviceCodeFlow )
239
213
{
240
214
// Fetch available accounts from 'app' instance
241
215
System . Collections . Generic . IEnumerator < IAccount > accounts = ( await app . GetAccountsAsync ( ) . ConfigureAwait ( false ) ) . GetEnumerator ( ) ;
@@ -276,15 +250,54 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
276
250
// An 'MsalUiRequiredException' is thrown in the case where an interaction is required with the end user of the application,
277
251
// for instance, if no refresh token was in the cache, or the user needs to consent, or re-sign-in (for instance if the password expired),
278
252
// or the user needs to perform two factor authentication.
279
- result = await AcquireTokenInteractiveDeviceFlowAsync ( app , scopes , parameters . ConnectionId , parameters . UserId , parameters . AuthenticationMethod , cts ) . ConfigureAwait ( false ) ;
280
- SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token (interactive) for {0} auth mode. Expiry Time: {1}" , parameters . AuthenticationMethod , result ? . ExpiresOn ) ;
253
+ if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryInteractive ||
254
+ parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryDeviceCodeFlow )
255
+ {
256
+ result = await AcquireTokenInteractiveDeviceFlowAsync ( app , scopes , parameters . ConnectionId , parameters . UserId , parameters . AuthenticationMethod , cts ) . ConfigureAwait ( false ) ;
257
+ SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token (interactive) for {0} auth mode. Expiry Time: {1}" , parameters . AuthenticationMethod , result ? . ExpiresOn ) ;
258
+ }
259
+ else
260
+ {
261
+ throw ;
262
+ }
281
263
}
282
264
}
283
265
else
284
266
{
285
- // If no existing 'account' is found, we request user to sign in interactively.
286
- result = await AcquireTokenInteractiveDeviceFlowAsync ( app , scopes , parameters . ConnectionId , parameters . UserId , parameters . AuthenticationMethod , cts ) . ConfigureAwait ( false ) ;
287
- SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token (interactive) for {0} auth mode. Expiry Time: {1}" , parameters . AuthenticationMethod , result ? . ExpiresOn ) ;
267
+ if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryIntegrated )
268
+ {
269
+ if ( ! string . IsNullOrEmpty ( parameters . UserId ) )
270
+ {
271
+ result = await app . AcquireTokenByIntegratedWindowsAuth ( scopes )
272
+ . WithCorrelationId ( parameters . ConnectionId )
273
+ . WithUsername ( parameters . UserId )
274
+ . ExecuteAsync ( cancellationToken : cts . Token )
275
+ . ConfigureAwait ( false ) ;
276
+ }
277
+ else
278
+ {
279
+ result = await app . AcquireTokenByIntegratedWindowsAuth ( scopes )
280
+ . WithCorrelationId ( parameters . ConnectionId )
281
+ . ExecuteAsync ( cancellationToken : cts . Token )
282
+ . ConfigureAwait ( false ) ;
283
+ }
284
+ SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token for Active Directory Integrated auth mode. Expiry Time: {0}" , result ? . ExpiresOn ) ;
285
+ }
286
+ else if ( parameters . AuthenticationMethod == SqlAuthenticationMethod . ActiveDirectoryPassword )
287
+ {
288
+ result = await app . AcquireTokenByUsernamePassword ( scopes , parameters . UserId , parameters . Password )
289
+ . WithCorrelationId ( parameters . ConnectionId )
290
+ . ExecuteAsync ( cancellationToken : cts . Token )
291
+ . ConfigureAwait ( false ) ;
292
+
293
+ SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token for Active Directory Password auth mode. Expiry Time: {0}" , result ? . ExpiresOn ) ;
294
+ }
295
+ else
296
+ {
297
+ // If no existing 'account' is found, we request user to sign in interactively.
298
+ result = await AcquireTokenInteractiveDeviceFlowAsync ( app , scopes , parameters . ConnectionId , parameters . UserId , parameters . AuthenticationMethod , cts ) . ConfigureAwait ( false ) ;
299
+ SqlClientEventSource . Log . TryTraceEvent ( "AcquireTokenAsync | Acquired access token (interactive) for {0} auth mode. Expiry Time: {1}" , parameters . AuthenticationMethod , result ? . ExpiresOn ) ;
300
+ }
288
301
}
289
302
}
290
303
else
0 commit comments