示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SshSetRequest"/> class.
 /// </summary>
 /// <param name="credentialName">Name of credential</param>
 /// <param name="privateKey">Private key for the credential</param>
 /// <param name="publicKey">Public key for the credential</param>
 public SshSetRequest(string credentialName, string privateKey, string publicKey)
 {
     Name  = credentialName;
     Type  = CredentialType.SSH;
     Value = new SshCredential {
         PrivateKey = privateKey, PublicKey = publicKey
     };
 }
        public SshCredential GetSshCredential(IAzureContext context, RSAParameters rsaKeyInfo)
        {
            if (!AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out PowerShellTokenCacheProvider tokenCacheProvider))
            {
                throw new NullReferenceException(Resources.AuthenticationClientFactoryNotRegistered);
            }

            var    publicClient = tokenCacheProvider.CreatePublicClient();
            string cloudName    = context.Environment.Name.ToLower();
            string scope        = CloudToScope.GetValueOrDefault(cloudName, null);

            if (scope == null)
            {
                throw new Exception(string.Format("Unsupported cloud {0}. Supported clouds include AzureCloud,AzureChinaCloud,AzureUSGovernment.", cloudName));
            }
            List <string> scopes = new List <string>()
            {
                scope
            };
            var jwk = CreateJwk(rsaKeyInfo, out string keyId);

            var account = publicClient.GetAccountAsync(context.Account.ExtendedProperties["HomeAccountId"])
                          .ConfigureAwait(false).GetAwaiter().GetResult();
            var result = publicClient.AcquireTokenSilent(scopes, account)
                         .WithSSHCertificateAuthenticationScheme(jwk, keyId)
                         .ExecuteAsync();
            var accessToken = result.ConfigureAwait(false).GetAwaiter().GetResult();

            var resultToken = new SshCredential()
            {
                Credential = accessToken.AccessToken,
                ExpiresOn  = accessToken.ExpiresOn,
            };

            return(resultToken);
        }