@@ -103,6 +103,9 @@ public void AssertUnrecoverableStateCountIsCorrect()
103
103
104
104
internal sealed class SqlInternalConnectionTds : SqlInternalConnection , IDisposable
105
105
{
106
+ // https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/retry-after#simple-retry-for-errors-with-http-error-codes-500-600
107
+ internal const int MsalHttpRetryStatusCode = 429 ;
108
+
106
109
// CONNECTION AND STATE VARIABLES
107
110
private readonly SqlConnectionPoolGroupProviderInfo _poolGroupProviderInfo ; // will only be null when called for ChangePassword, or creating SSE User Instance
108
111
private TdsParser _parser ;
@@ -2421,7 +2424,7 @@ internal SqlFedAuthToken GetFedAuthToken(SqlFedAuthInfo fedAuthInfo)
2421
2424
// Deal with Msal service exceptions first, retry if 429 received.
2422
2425
catch ( MsalServiceException serviceException )
2423
2426
{
2424
- if ( 429 == serviceException . StatusCode )
2427
+ if ( serviceException . StatusCode == MsalHttpRetryStatusCode )
2425
2428
{
2426
2429
RetryConditionHeaderValue retryAfter = serviceException . Headers . RetryAfter ;
2427
2430
if ( retryAfter . Delta . HasValue )
@@ -2440,9 +2443,15 @@ internal SqlFedAuthToken GetFedAuthToken(SqlFedAuthInfo fedAuthInfo)
2440
2443
}
2441
2444
else
2442
2445
{
2443
- break ;
2446
+ SqlClientEventSource . Log . TryTraceEvent ( "<sc.SqlInternalConnectionTds.GetFedAuthToken.MsalServiceException error:> Timeout: {0}" , serviceException . ErrorCode ) ;
2447
+ throw SQL . ActiveDirectoryTokenRetrievingTimeout ( Enum . GetName ( typeof ( SqlAuthenticationMethod ) , ConnectionOptions . Authentication ) , serviceException . ErrorCode , serviceException ) ;
2444
2448
}
2445
2449
}
2450
+ else
2451
+ {
2452
+ SqlClientEventSource . Log . TryTraceEvent ( "<sc.SqlInternalConnectionTds.GetFedAuthToken.MsalServiceException error:> {0}" , serviceException . ErrorCode ) ;
2453
+ throw ADP . CreateSqlException ( serviceException , ConnectionOptions , this , username ) ;
2454
+ }
2446
2455
}
2447
2456
// Deal with normal MsalExceptions.
2448
2457
catch ( MsalException msalException )
@@ -2453,21 +2462,7 @@ internal SqlFedAuthToken GetFedAuthToken(SqlFedAuthInfo fedAuthInfo)
2453
2462
{
2454
2463
SqlClientEventSource . Log . TryTraceEvent ( "<sc.SqlInternalConnectionTds.GetFedAuthToken.MSALException error:> {0}" , msalException . ErrorCode ) ;
2455
2464
2456
- // Error[0]
2457
- SqlErrorCollection sqlErs = new ( ) ;
2458
- sqlErs . Add ( new SqlError ( 0 , ( byte ) 0x00 , ( byte ) TdsEnums . MIN_ERROR_CLASS , ConnectionOptions . DataSource , StringsHelper . GetString ( Strings . SQL_MSALFailure , username , ConnectionOptions . Authentication . ToString ( "G" ) ) , ActiveDirectoryAuthentication . MSALGetAccessTokenFunctionName , 0 ) ) ;
2459
-
2460
- // Error[1]
2461
- string errorMessage1 = StringsHelper . GetString ( Strings . SQL_MSALInnerException , msalException . ErrorCode ) ;
2462
- sqlErs . Add ( new SqlError ( 0 , ( byte ) 0x00 , ( byte ) TdsEnums . MIN_ERROR_CLASS , ConnectionOptions . DataSource , errorMessage1 , ActiveDirectoryAuthentication . MSALGetAccessTokenFunctionName , 0 ) ) ;
2463
-
2464
- // Error[2]
2465
- if ( ! string . IsNullOrEmpty ( msalException . Message ) )
2466
- {
2467
- sqlErs . Add ( new SqlError ( 0 , ( byte ) 0x00 , ( byte ) TdsEnums . MIN_ERROR_CLASS , ConnectionOptions . DataSource , msalException . Message , ActiveDirectoryAuthentication . MSALGetAccessTokenFunctionName , 0 ) ) ;
2468
- }
2469
- SqlException exc = SqlException . CreateException ( sqlErs , "" , this ) ;
2470
- throw exc ;
2465
+ throw ADP . CreateSqlException ( msalException , ConnectionOptions , this , username ) ;
2471
2466
}
2472
2467
2473
2468
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.SqlInternalConnectionTds.GetFedAuthToken|ADV> {0}, sleeping {1}[Milliseconds]" , ObjectID , sleepInterval ) ;
0 commit comments