You are developing a C# web application that runs on an Azure App Service. The application must retrieve database connection secrets from an Azure Key Vault. You have already enabled a system-assigned managed identity for the App Service.
You write the following code to access the Key Vault:
csharp
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
// ...
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());
var secret = await client.GetSecretAsync("DbConnectionString");
When you deploy and run the application in Azure, it fails to retrieve the secret and throws an exception indicating that access is forbidden.
Which of the following actions should you perform to resolve this error?
- AModify the C# code to pass the application client ID as a parameter to the constructor of DefaultAzureCredential.
- BCreate an App Registration in Microsoft Entra ID, grant it Key Vault permissions, and assign its credentials to the App Service application settings.
- Create an access policy in Azure Key Vault that grants the Get secret permission to the system-assigned managed identity of the App Service.Cevap
- DCreate an access policy in Azure Key Vault that grants the Get secret permission to the App Service's App Registration service principal instead of the system-assigned managed identity.
Cevap
Create an access policy in Azure Key Vault that grants the Get secret permission to the system-assigned managed identity of the App Service.
The correct answer is to create an access policy in Azure Key Vault that grants the Get secret permission to the system-assigned managed identity of the App Service. When the system-assigned managed identity is enabled, Azure automatically creates an enterprise application principal representing the App Service instance. DefaultAzureCredential automatically detects this identity when deployed to Azure and uses it to acquire tokens. However, the identity must be authorized to perform data plane operations on the Key Vault by defining an access policy or an RBAC role assignment.
Adım Adım Çözüm
Anahtar Kavram
Azure Managed Identities and Azure Key Vault Authorization
Tahmini Süre:1m 30s