@@ -131,8 +131,9 @@ public virtual async Task<ICredential> GetCredentialAsync(InputArguments input)
131131 Context . Trace . WriteLine ( $ "Looking for existing credential in store with service={ service } account={ input . UserName } ...") ;
132132
133133 // 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 ++ )
136137 {
137138 Context . Trace . WriteLine ( "Querying for existing credentials..." ) ;
138139 credential = Context . CredentialStore . Get ( service , input . UserName ) ;
@@ -141,28 +142,25 @@ public virtual async Task<ICredential> GetCredentialAsync(InputArguments input)
141142 Context . Trace . WriteLine ( "No existing credentials found." ) ;
142143 break ;
143144 }
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+ }
144151 else
145152 {
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 )
153155 {
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 ) ;
165158 }
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 ) ;
166164 }
167165 }
168166 Context . Trace . WriteLine ( "Creating new credential..." ) ;
0 commit comments