Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To access Azure Key Vault from VS2022 on Localhost, follow these steps:

  1. Create an Azure Key Vault: Go to the Azure portal and create a new Key Vault or use an existing one. Make sure to give appropriate permissions to the service principal or user to access the vault.

  2. Install Azure CLI: Install Azure CLI on your local machine if you have not already done so. Azure CLI can be installed from the official website.

  3. Log in to Azure: Open a command prompt or terminal window and log in to Azure by running the command "az login". It will prompt you to enter your Azure credentials.

  4. Get Azure Key Vault Details: Run the command "az keyvault show" to get details of your Key Vault. Note down the "id" of the Key Vault.

  5. Install Azure Key Vault Extension: Install Azure Key Vault extension for Visual Studio by going to Extensions and searching for "Azure Key Vault".

  6. Add Connected Services: In Visual Studio, right-click on your project, and select Add Connected Service. Select Azure Key Vault and sign in with your Azure credentials.

  7. Select Azure Key Vault to Connect: In the next window, select the Azure Key Vault you want to connect to by selecting the subscription, resource group, and Key Vault name.

  8. Access Secrets in Code: After successfully connecting, you can access the secrets in your code by using the Microsoft.Azure.KeyVault NuGet package. Use the following code snippet to access a secret:

using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;

var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("https://{keyvault-name}.vault.azure.net/secrets/{secret-name}").ConfigureAwait(false);
var secretValue = secret.Value;

Replace the {keyvault-name} and {secret-name} with your Key Vault name and the name of the secret respectively.

That's it! You can now access your Azure Key Vault from your localhost using Visual Studio.