Skip to content

Commit 8d2560d

Browse files
committed
Address throttling of token requests by calling AcquireTokenSilent in Integrated/Password flows when the account is already cached.
Addresses issue dotnet#1915
1 parent 22eb6c7 commit 8d2560d

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationProvider.cs

+48-35
Original file line numberDiff line numberDiff line change
@@ -206,36 +206,10 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
206206

207207
IPublicClientApplication app = GetPublicClientAppInstance(pcaKey);
208208

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)
239213
{
240214
// Fetch available accounts from 'app' instance
241215
System.Collections.Generic.IEnumerator<IAccount> accounts = (await app.GetAccountsAsync().ConfigureAwait(false)).GetEnumerator();
@@ -276,15 +250,54 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
276250
// An 'MsalUiRequiredException' is thrown in the case where an interaction is required with the end user of the application,
277251
// 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),
278252
// 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+
}
281263
}
282264
}
283265
else
284266
{
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+
}
288301
}
289302
}
290303
else

0 commit comments

Comments
 (0)