I have an app which uses certificates ( Uploaded the cert) in Azure Azure KeyVault.
I am trying to use asp.net application 4.7 to connect to key vault and retrieve the application Certificate and the Secret associate with it
Previously I used Microsoft.Azure.KeyVault and KeyVaultClient in combination with GetCertificateAsync and GetSecretAsync to pull the certificate and its secret.
However I recognized that Microsoft.Azure.KeyVault is deprecated so i researched and I found That I should use Azure.Security.KeyVault.Certificates instead.
I wrote the below code New Way:
using Azure.Security.KeyVault.Certificates; public static X509Certificate2 GetCertificateFromVault(string keyVaultName, string certificateName) {
var keyVaultUri = $"https://{keyVaultName}.vault.azure.net/";
var client = new CertificateClient(new Uri(keyVaultUri), new DefaultAzureCredential());
KeyVaultCertificateWithPolicy keyVaultCertificateWithPolicy = client.GetCertificate(certificateName);
return new X509Certificate2(keyVaultCertificateWithPolicy.Cer);
}
However I get error on calling client.GetCertificate(certificateName); in above code which is my new way Here is the error
Multiple exceptions were encountered while attempting to authenticate. ---> Azure.Identity.CredentialUnavailableException: E
nvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information.
https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage)\r\n at
Azure.Identity.EnvironmentCredential.d__12.MoveNext()\r\n
I have KeyVaultCertificateOfficer role and I can retrieve the certificates using my old way on the local machine so I am sure I do not have access issue.
I looked at developer guide it says that I need to create environment variables for AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID Is that the solution? I do not understand why? Do I need to do this on all my different environment like UAT and PROD and create these variables. I kind of dont like that so i think i missing something here.
If I absolutely need to create env variables also not sure what to use as AZURE_CLIENT_SECRET as I use Certificate not secret for my App. I saw AZURE_CLIENT_CERTIFICATE_PATH but that is the path to pfx in local machine right which we do not want that.
Maybe my understanding is totally wrong I am not sure. So any guidance would be much appreciated!