Tuesday, 4 May 2021

Create Azure Key Vault and Access in Asp.Net Core Application

Azure Key Vault is a cloud service that helps teams securely store connection string, passwords, etc, and access them. Today in the article we will learn how to create the Key Vault in Azure Portal and how to access secrets in our code. 


Step 1: Go to Azure Portal and search Key Vault and create it. 



 





Step 2: Enter Key Vault Name, Resource Group, Region, etc as mentioned the below screenshot.

Press Review + Create button. 


Step 3: It will check validation in the background and it will create a new Key Vault. 


Step 4: Click on Your Key Vault and go to Secrets and Press Generate/Import button.



 






Step 5: Create a new secret with any name. I have created a new secret dbpwd. Press Create button.



 







Till now, we have created the Key Vault in the Azure Portal and created the secret too. Let’s fetch this secret value in the Code. Let’s create a sample in Asp.Net Core API application and fetch the secret.


Create/Open Visual Studio Web/API application. In my example, I have created Asp.Net Core 3.1 Application.


 Step 1: Go to Program.cs, In line number 35, I have specified my Key Vault URL. You can specify your Key Vault URL. if you are not aware of your URL then go to Azure Portal and click on the Key Vault and see in the Overview Tab. You can find the URL.

 


Note: I have used default credentials in line 27. I have logged in to Visual studio with the same credentials as I'm logged in on the Azure portal.


Step 2: Go to Controller and fetch Key Vault Secret.













I have created a new endpoint, kV, and fetch the secret value.


Step 3: Run the API Project



 





You can see it fetched the secret value in the browser. This is all about this article. I hope you like it.





No comments:

Post a Comment

Implement Authorization in Swagger with Static Value in Header .Net 8

If you want an anonymous user should not run the APIs. To run your API Endpoints From Swagger / Postman / Code the user should pass the head...