/// <summary> /// Creates a certificate /// </summary> /// <param name="certificateName"> the name of the created certificate </param> /// <returns> The created certificate </returns> private static CertificateOperation CreateCertificate(out string certificateName) { var vaultAddress = inputValidator.GetVaultAddress(); certificateName = inputValidator.GetCertificateName(); // Create a self-signed certificate backed by a 2048 bit RSA key var policy = new CertificatePolicy { IssuerReference = new IssuerReference { Name = "Self", }, KeyProperties = new KeyProperties { Exportable = true, KeySize = 2048, KeyType = "RSA" }, SecretProperties = new SecretProperties { ContentType = "application/x-pkcs12" }, X509CertificateProperties = new X509CertificateProperties { Subject = "CN=KEYVAULTDEMO" } }; var tags = inputValidator.GetTags(); var certificateOperation = keyVaultClient.CreateCertificateAsync(vaultAddress, certificateName, policy, new CertificateAttributes { Enabled = true }, tags).GetAwaiter().GetResult(); Console.Out.WriteLine("Created certificate:---------------"); PrintoutCertificateOperation(certificateOperation); return(certificateOperation); }
/// <summary> /// Creates a certificate /// </summary> /// <param name="certificateName"> the name of the created certificate </param> /// <returns> The created certificate </returns> private static CertificateBundle CreateCertificate(out string certificateName) { var vaultAddress = inputValidator.GetVaultAddress(); certificateName = inputValidator.GetCertificateName(); // Create a self-signed certificate backed by a 2048 bit RSA key var policy = new CertificatePolicy { IssuerReference = new IssuerReference { Name = "Self", }, KeyProperties = new KeyProperties { Exportable = true, KeySize = 2048, KeyType = "RSA" }, SecretProperties = new SecretProperties { ContentType = "application/x-pem-file" //ContentType = "application/x-pkcs12" }, X509CertificateProperties = new X509CertificateProperties { Subject = "CN=KEYVAULTDEMO" } }; var tags = inputValidator.GetTags(); var name = certificateName; var pendingCertificate = Task.Run(() => keyVaultClient.CreateCertificateAsync(vaultAddress, name, policy, new CertificateAttributes { Enabled = true }, tags)).ConfigureAwait(false).GetAwaiter().GetResult(); // Certificate request is created. // Poll for certificate creation status until pending is complete. var pendingPollCount = 0; while (pendingPollCount < 11) { var pendingCertificateResponse = Task.Run(() => keyVaultClient.GetCertificateOperationAsync(vaultAddress, pendingCertificate.CertificateOperationIdentifier.Name)) .ConfigureAwait(false).GetAwaiter().GetResult(); PrintoutCertificateOperation(pendingCertificateResponse); if (0 == string.Compare(pendingCertificateResponse.Status, "InProgress", true)) { Console.Out.WriteLine("Waiting on the long running operation to complete creating the certificate..."); Thread.Sleep(TimeSpan.FromSeconds(5)); pendingPollCount++; continue; } if (0 == string.Compare(pendingCertificateResponse.Status, "Completed", true)) { var certBundle = Task.Run(() => keyVaultClient.GetCertificateAsync(pendingCertificateResponse.Target)).ConfigureAwait(false).GetAwaiter().GetResult(); Console.Out.WriteLine("Created certificate:---------------"); PrintoutCertificate(certBundle); return(certBundle); } throw new Exception(string.Format( "Polling on pending certificate returned an unexpected result. Error code = {0}, Error message = {1}", pendingCertificate.Error.Code, pendingCertificate.Error.Message)); } throw new Exception(string.Format("Pending certificate processing delayed")); }