/// <summary>
/// <para>Uses credentials to authenticate with the Azure tenant and acquire the necessary
/// access tokens to exchange for a VSTS personal access token.</para>
/// <para>Tokens acquired are stored in the secure secret stores provided during
/// initialization.</para>
/// </summary>
/// <param name="targetUri">The unique identifier for the resource for which access is to
/// be acquired.</param>
/// <param name="credentials">The credentials required to meet the criteria of the Azure
/// tenant authentication challenge (i.e. username + password).</param>
/// <param name="requestCompactToken">
/// <para>Requests a compact format personal access token; otherwise requests a standard
/// personal access token.</para>
/// <para>Compact tokens are necessary for clients which have restrictions on the size of
/// the basic authentication header which they can create (example: Git).</para>
/// </param>
/// <returns><see langword="true"/> if authentication and personal access token acquisition was successful; otherwise <see langword="false"/>.</returns>
public async Task <bool> NoninteractiveLogonWithCredentials(TargetUri targetUri, Credential credentials, bool requestCompactToken)
{
BaseSecureStore.ValidateTargetUri(targetUri);
Credential.Validate(credentials);
Trace.WriteLine("VstsAadAuthentication::NoninteractiveLogonWithCredentials");
try
{
TokenPair tokens;
if ((tokens = await this.VstsAuthority.AcquireTokenAsync(targetUri, this.ClientId, this.Resource, credentials)) != null)
{
Trace.WriteLine(" token acquisition succeeded");
this.StoreRefreshToken(targetUri, tokens.RefeshToken);
return(await this.GeneratePersonalAccessToken(targetUri, tokens.AccessToken, requestCompactToken));
}
}
catch (AdalException)
{
Trace.WriteLine(" token acquisition failed");
}
Trace.WriteLine(" non-interactive logon failed");
return(false);
}