In this article, let’s create a .NET console application to create and retrieve secrets from Azure Key Vault using .NET Client Library. Azure Key Vault is a Azure cloud service which provides secure storage and retrieval of various secrets. Secrets can be passwords, encryption keys, certificates for the applications and services, etc. After this exercise one will be able to create and retrieve secrets using .NET Client Library along with creation of Azure key vault resource using Azure CLI and assignment of role to Microsoft Entra (Azure AD) user name.
Following tasks are performed:
Create Azure Key Vault resources using Azure CLI.
Assign role to Microsoft Entra (Azure AD) user name using Azure CLI.
Create and retrieve secrets using .NET Client Library.
Sign in to Azure and run application.
Clean up resources.
Create Azure Key Vault resource using Azure CLI
Initial task is to create and make ready Azure Key Vault resources using Azure CLI (Azure Cloud Shell) for further usage. Click Cloud Shell button on the menu bar at the top right side of the Azure portal. It will launch an interactive shell which can be used to run the steps outlined in this article. Select a Bash environment. To use code editor In cloud shell toolbar go to Settings menu and than select Go to Classic version.
Create resource group
Resource group is logical container used to hold related resources. It includes resources which required to manage as a group. Key Vault belongs to a resource group. az group create command used to create a group.
az group create \
--name myResourceGroup2 \
--location eastus2 eastus2 is location code instead of it one can use their nearest region location.
Create Azure Key Vault resource
Once resource group is created next task is to create a Azure Key Vault resource that will be used in next section. az keyvault create command is used to create a Azure key value resource. It will take few minutes to complete the process.
az keyvault create \
--name myKeyVaultName2\
--resource-group myResourceGroup2\
--location eastus2 \ Assign role to Microsoft Entra (Azure AD) user name
It is important to assign roles to Microsoft Entra (Azure AD) user name. Roles will define what access and permissions a user has within an organization’s Microsoft cloud environment. Roles determine what resources and actions a user can perform. One by one perform below steps in Azure CLI.
Retrieve userPrincipalName
First, retrieve userPrincipalName from account as mentioned in following command. It retrieves the information about currently authenticated user including their UPN and represents that who the role will be assigned to.
userPrincipal2=$(az rest
--method GET
--url https://graph.microsoft.com/v1.0/me \
--headers 'Content-Type=application/json' \
--query userPrincipalName
--output tsv)Retrieve resource ID
Second, get the resource ID of key vault using below command. This will be used in next step to set the scope for role assignment. Resource ID sets scope for role assignment to specific key vault.
resourceID2=$(az keyvault show \
--resource-group myResourceGroup2 \
--name myKeyVaultName2 \
--query id \
--output tsv)Create and assign Key Vault Secrets Officer role
Finally, let's create and assign key vault secrets officer role using az role assignment create command. It will give permissions to manage next routines. userPrincipal and resourceID variables are created in first and second steps as above which are used in this command to retrieve actual value at command execution time.
az role assignment create \
--assignee $userPrincipal2\
--role "Key Vault Secrets Officer" \
--scope $resourceID2Key Vault Secrets Officer role: It is to be assigned to Microsoft Entra user before create and retrieve secret. It gives permissions to create, retrieve and delete secrets to user account i.e. it allows to create and read actions.
Key Vault Secrets User role: It allows to read action only.
Now, required basic resources are deployed and ready to use. So, let's create a .NET console app.
Create and retrieve secrets using .NET Client Library
Let’s set-up the .NET console application to create and retrieve the secrets from Azure Key Vault using .NET Client Library. Perform the following steps one by one in to Cloud Shell to achieve this.
.NET Project Setup
Set up a console project to add and fetch secrets using .NET Client Library.
Create project directory and change to that directory. First create a directory for the project using below command and move to that directory.
mkdir keyvault2
cd keyvault2Create .NET console application to add and fetch secrets through it. dotnet new console command is used to create a .NET console app.
dotnet new consoleInstall Packages - Identity and Secrets packages are required to work with key vault. Run dotnet add package command to add Azure.Identity and Azure.Security.KeyVault.Secrets packages in project to use the SDK.
dotnet add package Azure.Identity
dotnet add package Azure.Security.KeyVault.SecretsStarter code
Add the below starter code into the project and replace template code in Program.cs file by using editor in Cloud Shell.
Application editing - open Program.cs file to edit it in the Azure CLI using code command.
code Program.csCode replacing - replace existing code of Program.cs with the below code. This is the initial started code and will replace last four commented lines with actual code in next “Complete code” section.
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
// Replace KEYVAULT-NAME with actual Key Vault name
string keyVaultUrl = "https://KEYVAULT-NAME.vault.azure.net/";
// CREATE - CLIENT
// CREATE - MENU OPTIONS
// LIST - SECRETS
// CREATE - SECRETSave & close - Press ctrl + s to save your changes and continue.
Complete code
Add following code one by one at mentioned commented section to complete the application. Let’s update the code for commented lines for specific operations one by one as described to create client, add menu options, list out secrets, and add secretes to key vault.
Create Client - First create an instance of SecretClient class to add and fetch secrets using it. This class is main starting point to perform all the operations against Key Vault. Locate // CREATE CLIENT comment, then add following code directly beneath comment. KEYVAULT-NAME will be actual key value. DefaultAzureCredentialOptions configures authentication options. SecretClient creates Key Vault client
// CREATE - CLIENT
// Configure authentication options to connect with Azure Key Vault
DefaultAzureCredentialOptions defaultAzureCredentialOptions = new()
{
ExcludeEnvironmentCredential = true,
ExcludeManagedIdentityCredential = true
};
// Create Key Vault client through URL and authentication credentials
var secretClient = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential(defaultAzureCredentialOptions));
Join the conversation! Your thoughts help the community grow.