In the old days, we used to access the Azure Key Vaults using the Vault URL and its Secret Key, we were placing this in the config file and going from there.
Placing sensitive information in the config file is a bad idea, it may cause a security breach and loss of data.
This article will explain how we can access the Azure Key Vault information using Self-signed certification, which involves the below steps.
- Create self-signed certificate
- Create a private key for the certificate
- Create Azure AD App
- Assign certificate to Azure AD APP
- Provide permission to Azure AD app in Key Vault
- Register/publish the certificate on a local machine (In the current scenario we are storing the certificate in a local machine, we can register the certificate in Azure app service also)
Coming to actual implementation.
Step 1. Creating Self-Signed certificate
Open Visual Studio command prompt as administrator.
![Visual Studio]()
Execute the below command to create the certificate. It will prompt for the private key password; provide the same password in all the places.
![Command]()
![OK]()
Step 2. Create a private key for the certificate
Use the below command to create a private certificate. Here, we need to pass the certificate name and desired pfx file name and private key.
![Create]()
It created both, public and private, certificates.
![Certificates]()
Step 3 & 4. Create an Azure AD App and Assign a certificate
The below PowerShell script will import the certificate, create an Azure AD App, and assign the certificate to that app.
![App]()
![Settings]()
Step 4. Provide permission to the Azure AD app
The below script will give permission to the Azure AD App in Azure Key Vault.
![AD App]()
You can get the thumbprint of the certificate by using the Certificate Thumbprint function.
Now, we have done all the necessary configuration for registering the certification with Azure, and created an application to access the key vaults.
Step 5. Register certificate in Local Machine
Open the private certification (.psk file) and follow the below steps to register the certificate on a local machine.
![Local Machine]()
Select default options provide a private key and finish the wizard, this will register the certificate on a local machine. We can find the registered certificate in MMC (Microsoft Management Console).
Search for MMC and open.
![MMC]()
Open the File menu and click on Add/Remove Snap-in. Select Computer Account and Local Computer to add the certificate section.
![Local computer]()
![Azure Key]()
Open the Certificate folder. You can see all the registered certificates here.
![Certificate folder]()
Step 6. Creating an application to access the key vaults
Here, I am creating a simple Visual Studio console application for demo purposes.
Create a console application and add the below helper call to the project.
![Key vault]()
AzureKeyVaultCertificationHelper.cs
Program. cs
Provide certificate Thumbprint, Azure AD App client ID, and Vault URL.
app. config
Output
![Output]()