public AcquireToken ( |
||
targetUri | ||
username | string | |
password | string | |
authenticationCode | string | |
scope | ||
return | Task |
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// <param name="username">The username of the account for which access is to be acquired.</param> /// <param name="password">The password of the account for which access is to be acquired.</param> /// <param name="authenticationCode">The two-factor authentication code for use in access acquisition.</param> /// <returns>True if success; otherwise false.</returns> public async Task <bool> NoninteractiveLogonWithCredentials(TargetUri targetUri, string username, string password, string authenticationCode = null) { BaseSecureStore.ValidateTargetUri(targetUri); if (String.IsNullOrWhiteSpace(username)) { throw new ArgumentNullException("username", "The `username` parameter is null or invalid."); } if (String.IsNullOrWhiteSpace(password)) { throw new ArgumentNullException("username", "The `password` parameter is null or invalid."); } Trace.WriteLine("GitHubAuthentication::NoninteractiveLogonWithCredentials"); GitHubAuthenticationResult result; if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope)) { Trace.WriteLine(" token acquisition succeeded"); PersonalAccessTokenStore.WriteCredentials(targetUri, (Credential)result.Token); return(true); } Trace.WriteLine(" non-interactive logon failed"); return(false); }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// <param name="username">The username of the account for which access is to be acquired.</param> /// <param name="password">The password of the account for which access is to be acquired.</param> /// <param name="authenticationCode">The two-factor authentication code for use in access acquisition.</param> /// <returns>Acquired <see cref="Credential"/> if successful; otherwise <see langword="null"/>.</returns> public async Task <Credential> NoninteractiveLogonWithCredentials(TargetUri targetUri, string username, string password, string authenticationCode) { BaseSecureStore.ValidateTargetUri(targetUri); if (String.IsNullOrWhiteSpace(username)) { throw new ArgumentNullException("username", "The `username` parameter is null or invalid."); } if (String.IsNullOrWhiteSpace(password)) { throw new ArgumentNullException("username", "The `password` parameter is null or invalid."); } Credential credentials = null; GitHubAuthenticationResult result; if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope)) { Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded."); credentials = (Credential)result.Token; PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); return(credentials); } Git.Trace.WriteLine($"non-interactive logon for '{targetUri}' failed."); return(credentials); }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store 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">(out) Credentials when acquisition is successful; null otherwise.</param> /// <returns>True if success; otherwise false.</returns> public bool InteractiveLogon(TargetUri targetUri, out Credential credentials) { string username; string password; if (AcquireCredentialsCallback(targetUri, out username, out password)) { GitHubAuthenticationResult result; if (result = GitHubAuthority.AcquireToken(targetUri, username, password, null, this.TokenScope).Result) { Trace.WriteLine(" token acquisition succeeded"); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } return(true); } else if (result == GitHubAuthenticationResultType.TwoFactorApp || result == GitHubAuthenticationResultType.TwoFactorSms) { string authenticationCode; if (AcquireAuthenticationCodeCallback(targetUri, result, username, out authenticationCode)) { if (result = GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope).Result) { Trace.WriteLine(" token acquisition succeeded"); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } return(true); } } } // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } } Trace.WriteLine(" interactive logon failed"); credentials = null; return(false); }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// /// <returns>Acquired <see cref="Credential"/> if successful; otherwise <see langword="null"/>.</returns> public async Task <Credential> InteractiveLogon(TargetUri targetUri) { Credential credentials = null; string username; string password; if (AcquireCredentialsCallback(targetUri, out username, out password)) { GitHubAuthenticationResult result; if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, null, this.TokenScope)) { Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded"); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it AuthenticationResultCallback?.Invoke(targetUri, result); return(credentials); } else if (result == GitHubAuthenticationResultType.TwoFactorApp || result == GitHubAuthenticationResultType.TwoFactorSms) { string authenticationCode; if (AcquireAuthenticationCodeCallback(targetUri, result, username, out authenticationCode)) { if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope)) { Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded."); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it AuthenticationResultCallback?.Invoke(targetUri, result); return(credentials); } } } // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } } Git.Trace.WriteLine($"interactive logon for '{targetUri}' failed."); return(credentials); }