---
title: How to Store and Retrieve Secrets in Azure
description: Azure Key Vault enables the safe storage of API keys, passwords, certificates, database connection strings, and other secrets.
image: https://blog.e-zest.com/hubfs/Blog%20Graphic%20How%20to%20store%20and%20retrieve%20secret%20in%20Azure-03.jpg
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [News](https://blog.e-zest.com/news)
- [Podcasts](https://www.e-zest.com/podcasts)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [CSR](https://blog.e-zest.com/csr)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [Media Kit](https://www.e-zest.com/media-kit)

# How to Store and Retrieve Secrets in Azure

[Swapnil Thitame](https://blog.e-zest.com/author/swapnil-thitame)  Mar 06, 2023

[security](https://blog.e-zest.com/topic/security) [Azure Cloud](https://blog.e-zest.com/topic/azure-cloud) [Secrets](https://blog.e-zest.com/topic/secrets) [Azure Key Vault](https://blog.e-zest.com/topic/azure-key-vault) [API Keys](https://blog.e-zest.com/topic/api-keys)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure&text=How%20to%20Store%20and%20Retrieve%20Secrets%20in%20Azure>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure>

Azure Key Vault is a cloud service that provides a secure place to store secrets. It enables the safe storage of API (Application Program Interface) keys, passwords, certificates, database connection strings, and other secrets. The Azure portal is used to build and manage Azure Key Vaults.

Following are the steps to store the secrets in Azure Key Vaults:

1. Click on the link to login into the Azure portal: [https://portal.azure.com/](https://portal.azure.com/)
   
   ![Create Resource in Azure](https://blog.e-zest.com/hs-fs/hubfs/Image1.png?width=688&height=389&name=Image1.png)
2. Click on create a resource or search key vault in the search box.
3. Select Key Vault from the results.
4. Select ‘Create'.
   
   ![Create key vault in azure](https://blog.e-zest.com/hs-fs/hubfs/Image2.png?width=559&height=346&name=Image2.png)
5. Provide a Name for the resource group (i.e., Dev/QA/Prod) and key vaults name (i.e., dev-kv) Click on the review and create button.
6. Once created key vaults (i.e., dev-kv) go to these key vaults and open settings.
7. In the settings section, click on secret then Generate/Import secret.
   
   ![import Secret in azure](https://blog.e-zest.com/hs-fs/hubfs/Image3.png?width=621&height=297&name=Image3.png)
8. In the last step, a window will be displayed to generate a secret. Add the Secret name (ConnectionString) and its value (UserID=postgres;Password=123;Host=localhost;Port=5432;Database=PostgreSqlDemoDb).
   
   ![Create secret in azure](https://blog.e-zest.com/hs-fs/hubfs/Image4.png?width=511&height=298&name=Image4.png)

By using above steps, we can create key vaults for Dev, QA, and Production environment so they can be easily maintained.

How to retrieve secret (ConnectionString, Password etc.) from Azure key vaults in C#.

There are two ways for retrieving a secret from Azure key vaults in C#.

- Using Client Credentials: -
  
  By using client credentials (App\_Client\_id, App\_Client\_secret, KeyVaultUri) can retrieve secrete from azure key vaults.
  
  using System;  
  using System.Collections.Generic;  
  using System.Linq;  
  using System.Text;  
  using System.Threading.Tasks;  
  using System.Web.Mvc;  
  using Microsoft.AspNetCore.DataProtection;  
  using Microsoft.Azure.KeyVault;  
  using Microsoft.IdentityModel.Clients.ActiveDirectory; 
  
   namespace RetrieveKeyUsingCredential
  
  {
  
      internal class SecurityController: Controller
  
      {
  
          private readonly IDataProtector \_dataProtector;
  
          public SecurityController(IDataProtectionProvider dataprovider)
  
          {
  
              this.\_dataProtector = dataprovider.CreateProtector("POC");
  
          }
  
          public void Main()
  
          {
  
              // Add below actual client credentials.
  
              String App\_Client\_id = "";
  
              String App\_Client\_secret = "";
  
              String KeyVaultUri = "";
  
              String ClientidEncrypt = this.\_dataProtector.Protect(App\_Client\_id);
  
              String ClientsecretEncrypt = this.\_dataProtector.Protect(App\_Client\_secret);
  
              string Clientiddecrypt = this.\_dataProtector.Unprotect(ClientidEncrypt);
  
              String Clientsecretdecrypt = this.\_dataProtector.Unprotect(ClientsecretEncrypt);
  
              //Above code used for encrypt and decrypt the client id.
  
              var kvc = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(async (string authority, string
  
                             resource, string scope) =\>
  
               {
  
                  var authContext = new AuthenticationContext(authority);
  
                  var credential = new ClientCredential(Clientiddecrypt, Clientsecretdecrypt);
  
                  AuthenticationResult result = await authContext.AcquireTokenAsync(resource, credential);
  
                  if (result == null)
  
                  {
  
                      throw new InvalidOperationException("Failed to retrieve secret from Azure key vaults");
  
                  }
  
                  return result.AccessToken;
  
              }
  
              ));
  
              var secretBundle = kvc.GetSecretAsync(KeyVaultUri, "ConnectionString").Result;
  
              //ConnectionString:Name of secret is case sensitive. The name should be same which is added in key vaults. 
  
              Console.WriteLine("Connection String Secret Value:" + secretBundle.Value);
  
              Console.ReadKey();
  
          }
  
      }
  
  }
- Without Client Credentials:-

 Without client credentials, we can retrieve a secret from Azure Key Vault but the developer/user should have access to key vaults. Following are steps for adding read-only access for Key Vaults.

- Go to key vaults (dev-kv)
- Click on Access policies
- Add user and give access to retrieve the secret

**Source code:**

using System;

using Azure.Security.KeyVault.Secrets;

using Azure.Identity;

string KeyVaultURL = ""; 

var secretclient = new SecretClient(new Uri(KeyVaultURL), new DefaultAzureCredential());

var connectionString = await secretclient.GetSecretAsync("ConnectionString");

 //ConnectionString:Name of secret is case sensitive. The name should be same which is added in key vaults.

String SecretValue = connectionString.Value.Value;

Console.WriteLine("Connection String secret value:" + SecretValue);

Console.ReadKey();

We can conclude, referencing Azure Key Vault secrets is a safe and secure way to use credentials.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure&text=How%20to%20Store%20and%20Retrieve%20Secrets%20in%20Azure>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/how-to-store-and-retrieve-secrets-in-azure&title=&summary=&source=>

### Similar Blog

[![DevSecOps – An Approach to Security](https://blog.e-zest.com/hubfs/DevSecOps%20v1-03.jpg) DevSecOps – An Approach to Security DevSecOps stands for development, security, and operations. The term "DevSecOps" has recently created a lot of buzz in the ... By Mohini Wable Nov 02, 2022](https://blog.e-zest.com/devsecops-an-approach-to-security)

[![Insightful Tips CIOs must not Ignore](https://blog.e-zest.com/hubfs/Blog%20Graphics%20(Insightful%20tips%20CIOs%20must%20not%20ignore%20in%202017).jpg) Insightful Tips CIOs must not Ignore CIOs of enterprises are under tremendous pressure to innovate and evolve according to the ever changing business dynamics. CIOs ... By Nidhi Thakkar Jan 27, 2017](https://blog.e-zest.com/insightful-tips-cios-must-not-ignore-in-2017)

[![SharePoint - Simplifying the Life of Government](https://blog.e-zest.com/hubfs/Blog%20Graphic%20(SharePoint%20-%20Simplifying%20the%20life%20of%20government).jpg) SharePoint - Simplifying the Life of Government SharePoint is one of the widely used platform, especially in the government sector, because of its collaboration and content ... By Nidhi Thakkar Jan 11, 2017](https://blog.e-zest.com/sharepoint-simplifying-the-life-of-government)

[![Technology stories you cannot miss – Week 43, 2016](https://blog.e-zest.com/hubfs/Tech_Digest_Week_43.png) Technology stories you cannot miss – Week 43, 2016 By Corporate Oct 21, 2016](https://blog.e-zest.com/technology-stories-you-cannot-miss-week-43-2016)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.