I want to add Azure Key Vault to my C# app but I'd like to run the code locally. Is there a possible way? A lot of documentation says that AKV only works on Azure env but here a Microsoft developer says in the video that the sample code they are showing in the demo can be executed in localhost too.
Currently using the Net6 version.
To create my Azure resources I followed the video tutorial from the first link. They follow this schema.
So here you can see my Key Vault access policies. RBAC is checked.
The Managed Identity has a role assigned to the Key Vault as Key Vault Administrator (for testing purpose)
My Managed Identity is assigned as User Assigned Managed Identity in my App Service on Identity tab.
My Program.cs looks like this:
var userAssignedClientId = "Some guid";
if (!string.IsNullOrEmpty(MyVaultUri))
{
builder.Configuration.AddAzureKeyVault(
new Uri(MyVaultUri),
new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = userAssignedClientId }));
}
I have declared the env variables AZURE_TENANT_ID
, AZURE_CLIENT_ID
, AZURE_CLIENT_SECRET
and the nugget packages Azure.Identity v1.4.0, Azure.Security.KeyVault.Secrets v4.3.0 installed.
And this is what I'm getting as error in localhost: Azure.RequestFailedException: 'Caller is not authorized to perform action on resource.