@@ -126,18 +126,53 @@ private static string GetServiceName(Uri baseUri)
126
126
127
127
public async Task < ICredential > GetCredentialAsync ( InputArguments input )
128
128
{
129
- // Try and locate an existing credential in the OS credential store
130
129
string service = GetServiceName ( input ) ;
131
- _context . Trace . WriteLine ( $ "Looking for existing credential in store with service={ service } account={ input . UserName } ...") ;
130
+ Uri remoteUri = input . GetRemoteUri ( ) ;
131
+
132
+ // If we have a specific username then we can try and find an existing credential for that account.
133
+ // If not, we should check what accounts are available in the store and prompt the user if there
134
+ // are multiple options.
135
+ string userName = null ;
136
+ bool addAccount = false ;
137
+ if ( string . IsNullOrWhiteSpace ( input . UserName ) )
138
+ {
139
+ IList < string > accounts = _context . CredentialStore . GetAccounts ( service ) ;
140
+ _context . Trace . WriteLine ( $ "Found { accounts . Count } accounts in the store for service={ service } .") ;
141
+
142
+ switch ( accounts . Count )
143
+ {
144
+ case 1 :
145
+ userName = accounts [ 0 ] ;
146
+ break ;
147
+
148
+ case > 1 :
149
+ userName = await _gitHubAuth . SelectAccountAsync ( remoteUri , accounts ) ;
150
+ addAccount = userName is null ;
151
+ break ;
152
+ }
153
+ }
154
+
155
+ // Always try and locate an existing credential in the OS credential store
156
+ // unless we're being told to explicitly add a new account. If the account lookup
157
+ // failed above we should still try to lookup an existing credential.
158
+ ICredential credential = null ;
159
+ if ( addAccount )
160
+ {
161
+ _context . Trace . WriteLine ( "Adding a new account!" ) ;
162
+ }
163
+ else
164
+ {
165
+ _context . Trace . WriteLine ( $ "Looking for existing credential in store with service={ service } account={ userName } ...") ;
166
+ credential = _context . CredentialStore . Get ( service , userName ) ;
167
+ }
132
168
133
- ICredential credential = _context . CredentialStore . Get ( service , input . UserName ) ;
134
169
if ( credential == null )
135
170
{
136
171
_context . Trace . WriteLine ( "No existing credentials found." ) ;
137
172
138
173
// No existing credential was found, create a new one
139
174
_context . Trace . WriteLine ( "Creating new credential..." ) ;
140
- credential = await GenerateCredentialAsync ( input . GetRemoteUri ( ) , input . UserName ) ;
175
+ credential = await GenerateCredentialAsync ( remoteUri , input . UserName ) ;
141
176
_context . Trace . WriteLine ( "Credential created." ) ;
142
177
}
143
178
else
0 commit comments