You are developing a secure .NET web API hosted on an Azure App Service. The API needs to programmatically retrieve an X.509 certificate, including its private key, from an Azure Key Vault named `kv-prod` to sign outgoing requests.
The App Service is configured with a system-assigned managed identity and has been assigned only the 'Key Vault Secrets User' Azure RBAC role on `kv-prod`.
Which C# code segment should you use to retrieve the certificate along with its private key?
- var client = new SecretClient(new Uri("https://kv-prod.vault.azure.net/"), new DefaultAzureCredential());
KeyVaultSecret secret = await client.GetSecretAsync("SigningCert");
var certificate = new X509Certificate2(Convert.FromBase64String(secret.Value));Cevap - Bvar client = new CertificateClient(new Uri("https://kv-prod.vault.azure.net/"), new DefaultAzureCredential());
KeyVaultCertificateWithPolicy cert = await client.GetCertificateAsync("SigningCert");
var certificate = new X509Certificate2(cert.Cer); - Cvar client = new KeyClient(new Uri("https://kv-prod.vault.azure.net/"), new DefaultAzureCredential());
KeyVaultKey key = await client.GetKeyAsync("SigningCert");
var certificate = new X509Certificate2(key.Key.N); - Dvar credential = new ClientSecretCredential("tenantId", "clientId", "clientSecret");
var client = new SecretClient(new Uri("https://kv-prod.vault.azure.net/"), credential);
KeyVaultSecret secret = await client.GetSecretAsync("SigningCert");
var certificate = new X509Certificate2(Convert.FromBase64String(secret.Value));
Cevap
Use SecretClient with DefaultAzureCredential to retrieve the certificate value as a secret, then instantiate the X509Certificate2 object using the base64-decoded bytes.
In Azure Key Vault, when an X.509 certificate is created or imported, the certificate's private key and full PFX/PEM contents are stored as a Secret with the same name. To retrieve the private key of a certificate programmatically, you must retrieve it as a secret using the SecretClient and decode the base64-encoded secret value. Because the App Service managed identity is granted the 'Key Vault Secrets User' RBAC role, it has the necessary permissions to read secrets from the Key Vault.
Adım Adım Çözüm
Anahtar Kavram
Retrieving certificates with private keys from Azure Key Vault using the Azure SDK for .NET and Azure RBAC
Tahmini Süre:1m 30s