I am trying to read/write secrets from Azure Key Vault, in a C# Console App. You can see the code below, but I basically just followed the Microsoft Quick Start Guide. I have used this before, successfully, on a Windows Machine. But when I do it on my MacBook (M1), I just get an error:
Unhandled exception. Azure.Identity.CredentialUnavailableException: DefaultAzureCredential failed to retrieve a token from the included credentials. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/defaultazurecredential/troubleshoot
- EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot
- ManagedIdentityCredential authentication unavailable. Multiple attempts failed to obtain a token from the managed identity endpoint.
- Operating system Darwin 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:35 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T8101 isn't supported.
- Stored credentials not found. Need to authenticate user in VSCode Azure Account. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/vscodecredential/troubleshoot
- Azure CLI not installed
- PowerShell is not installed.
I see that it's complaining about a lot of missing stuff, but how do I fix that? I have installed the AZ CLI Tool and I have run az login in the terminal (and verified that it works). I have even managed to list my key vaults, with the CLI Tool, so it's not that there is no connection.
Update: It turns out that I only get this error when running in VS Code. When I switch to Visual Studio, it seems to work.
Minimum code example:
Console.WriteLine("Attempting to read from Key Vault...");
string keyVaultName = "<key-vault-name>";
var kvUri = "https://" + keyVaultName + ".vault.azure.net";
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
string secretName = "<secret-name>";
var secret = await client.GetSecretAsync(secretName);
Console.WriteLine(secret.Value.ToString());

