@@ -131,8 +131,9 @@ public virtual async Task<ICredential> GetCredentialAsync(InputArguments input)
131
131
Context . Trace . WriteLine ( $ "Looking for existing credential in store with service={ service } account={ input . UserName } ...") ;
132
132
133
133
// Query for matching credentials
134
- ICredential credential = null ;
135
- while ( true )
134
+ ICredential credential ;
135
+ // Limit iterations to avoid infinite loop if CredentialStore.Remove misbehaves
136
+ for ( int i = 0 ; i < 3 ; i ++ )
136
137
{
137
138
Context . Trace . WriteLine ( "Querying for existing credentials..." ) ;
138
139
credential = Context . CredentialStore . Get ( service , input . UserName ) ;
@@ -141,28 +142,25 @@ public virtual async Task<ICredential> GetCredentialAsync(InputArguments input)
141
142
Context . Trace . WriteLine ( "No existing credentials found." ) ;
142
143
break ;
143
144
}
145
+ Context . Trace . WriteLine ( "Existing credential found." ) ;
146
+ if ( await ValidateCredentialAsync ( input . GetRemoteUri ( ) , credential ) )
147
+ {
148
+ Context . Trace . WriteLine ( "Existing credential satisfies validation." ) ;
149
+ return credential ;
150
+ }
144
151
else
145
152
{
146
- Context . Trace . WriteLine ( "Existing credential found." ) ;
147
- if ( await ValidateCredentialAsync ( input . GetRemoteUri ( ) , credential ) )
148
- {
149
- Context . Trace . WriteLine ( "Existing credential satisfies validation." ) ;
150
- return credential ;
151
- }
152
- else
153
+ Context . Trace . WriteLine ( "Existing credential fails validation." ) ;
154
+ if ( credential . OAuthRefreshToken != null )
153
155
{
154
- Context . Trace . WriteLine ( "Existing credential fails validation." ) ;
155
- if ( credential . OAuthRefreshToken != null )
156
- {
157
- Context . Trace . WriteLine ( "Found OAuth refresh token." ) ;
158
- input = new InputArguments ( input , credential . OAuthRefreshToken ) ;
159
- }
160
- Context . Trace . WriteLine ( "Erasing invalid credential..." ) ;
161
- // Why necessary to erase? We can't be sure that storing a fresh
162
- // credential will overwrite the invalid credential, particularly
163
- // if the usernames differ.
164
- Context . CredentialStore . Remove ( service , credential ) ;
156
+ Context . Trace . WriteLine ( "Found OAuth refresh token." ) ;
157
+ input = new InputArguments ( input , credential . OAuthRefreshToken ) ;
165
158
}
159
+ Context . Trace . WriteLine ( "Erasing invalid credential..." ) ;
160
+ // Why necessary to erase? We can't be sure that storing a fresh
161
+ // credential will overwrite the invalid credential, particularly
162
+ // if the usernames differ.
163
+ Context . CredentialStore . Remove ( service , credential ) ;
166
164
}
167
165
}
168
166
Context . Trace . WriteLine ( "Creating new credential..." ) ;
0 commit comments