Summary:
Very good article. How to integrate web app with key vault and clear steps on how to set up permissions .
Further Reading:
New features in .net 8 and accessing secrets
Implementing Azure Key Vault in C# for Secure Configuration

In this article, I will explain securing the secrets, passwords, connection strings, etc., using the Azure key vault. and integrating the same Azure key vault in the .net web API application using managed identity to access the securely stored credentials in the Azure key vault. So let's begin with what an Azure key vault means and its need.
What is Azure key vault, and why would we need it?
Secrets are anything you want to control access to, like client secrets, API keys, passwords, certificates, or cryptographic keys. Nowadays (for various reasons) everyone wants to store secrets securely where any unauthorized entity can't access them. It becomes more challenging when we have to manage secrets in the cloud. That's where the 'Azure key vault' comes to the rescue. It is one of the most popular Azure offerings used to store secrets and credentials much more securely.
We should not store our secrets and credentials in the config file as everyone can access it, and it will also be exposed to the repo level. We should store those securely.
How to access it?
To perform any operations with the key vault or if you want to access it in your application, you need to be authenticated first. This can be achieved in three ways.
- Managed Identity
2. Using service principals and certificates.
3. Using service principal and secret.
In this article, we will see how we can connect the Azure key vault from a web app using Managed Identity, the latest, most popular, and easy way to access secrets from the key vault without writing a lot of code.
The prerequisites are:
- .NET 6 (I have used .NET 6, but you can use .NET core 5 or .NET core 3.1)
- Azure key vault created in Azure portal.
- Web app created in an Azure portal where we will deploy our code.
- Visual Studio 2022 (You can use 2019 also for earlier .NET core versions).
Access secrets from a web app using the managed identity
Managed identity is a concept in the Microsoft Azure cloud that eliminates the need for developers to manage credentials after deploying their applications. With managed identity, we can access different Azure services securely without storing credentials like connection strings or passwords.
There are two types of managed identities.
- System assigned — it is created as a part of Azure resources like Azure app service and virtual machine. When we delete the resource, related managed identity is also deleted, which means it shares a life cycle.
- User-assigned managed identity — created as a stand-alone Azure resource that does not share a life cycle and has to be explicitly deleted.
We will use a system-assigned managed identity for this article/implementation. Let's begin by creating a web app to host our web API created with .NET 6.
Create a Web app in the Azure portal
- Navigate to the Azure portal's home page and click Create Resource from the left menu.

2. From the list, select Web app

3. As shown in the below screen, provide the details.
- Subscription
- resource group
- name — the name of the web app, which should be unique
- publish — select 'Code' as we will publish our code on the web app.
- Runtime stack — .NET 6 LTS
- Operating system — Windows
- Region — Central US (you can provide others if you want).
Like this, provide the details.

Enable Azure Managed Identity
Now we have created a web app in Azure, let's enable managed identity for that web app.

As above screen,
- Go to the created web app and navigate to the Identity section from the left menu under the settings option. You will see System assigned and a user assigned, Select the system assigned and toggle that status button to 'On'.
- Once you enable the managed identity, you will see the Object ID created on the above screen; copy that, as we will need it for granting access to the key vault.
Create Azure key vault
Like the web app, When we click the Create resource button, we will see the Key Vault option below.

Create the key vault by providing the details.

Once you create a key vault, you will get one key vault URI which will be used to integrate the key vault into the web app.

We have created a web app and its managed identity, Azure key vault. The Next step is to grant access to the Azure key vault to the web app created above using the access policy.
Grant access to the secret
From the left menu of the Key Vault page, select Access Policies, and then click Create, as shown in the following picture:


- From the 'Configure from template' dropdown, select The 'Key, Secret & Certificate management' option.
- Then select Get and List as shown above. This will allow us to list and read the secret values stored in the Azure Key Vault instance.
- Click Next, and you will see the 'principal' tab; in the search box, provide the object ID of created managed identity of a web app. Once you enter the object ID, the web app name will get displayed; select that as shown below.

Click Next, Next, and finally, Create. It will create an access policy for the key vault, which tells us that the web app can now access the key vault secrets using the managed identity.
