I am trying to retrieve a secret from Azure Key Vault (without using credentials, like in this tutorial: example):
public static async Task<string> GetSecret(string secretName)
{
try
{
return (await GetClient().GetSecretAsync(keyVaultUrl, secretName)).Value;
}
catch (KeyVaultErrorException)
{
return null;
}
catch (Exception ex)
{
return null;
}
}
public static async Task<string> GetAccessTokenAsync()
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
return await azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net");
}
private static KeyVaultClient GetClient()
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
using KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
return keyVaultClient;
}
but when I call GetSecret()
I get an
object reference not set to an instance of an object
exception.
I am pretty sure that the secret does in fact exist in the key vault.
My key vault url is formatted like "https://my-keyvault.vault.azure.net".
using
in the GetClient() function. It means the client is disposed before returning it. Put the using in the code where you are using the client, not in the GetClient() function. – juunas