Are you looking to authorize your Key Vault secrets to Serverless Azure Function? If yes, then this article is for you. In the previous article, we learned how to access and configure Key Vault in our ASP.NET Core web application. Wait! What if we want to access the Key Vault from a Serverless Azure Function?
Before diving in, we have already set up a Key Vault on the Azure portal and now, we want to access the stored secrets on Key Vault in our Azure Function.
However, you are probably wondering: "How do I use it?" Can I access the Key Vault in the same way as I did in the ASP.NET CORE web application?
We can access the Key Vault Secret in our Azure Function in two different ways
- Application settings from Key Vault@Microsoft.KeyVault(SecretUri=secret_uri_with_version) Allowing Application settings to access KeyVault via secret URL
- Managed Identities for App Services By giving access rights to our Azure Function to access the Key Vault secrets with the help of Secret URIhttps://{name}.vault.azure.net/secrets/{secretname})
Case Study
Let us look at an example where we want to access some secret keys in our Azure function to achieve our business requirement. Considering that we are trying to solve a business problem with this HTTP Trigger Azure function, let us do a proof of concept that the Azure function should be an HTTP trigger Azure function. Once the user tries to consume the Azure function, it should show those secret values in the response.
Here are the 3 requirements that you will discover,
-
Configure the Azure function to communicate with Key Vault when deployed on the local development environment and Azure.
-
Locate where to store keys in the local development environment.
-
Learn how to Deploy Azure function on Azure.
Prerequisite
-
Active Azure subscription
-
Azure Resources needed
-
Azure Function
-
Azure Active Directory
-
Key Vault
-
How do we proceed?
Breaking down the problem into smaller chunks first that’s how we will be proceeding ahead step by step,
- Create your first HTTP Trigger Azure function. If you are not aware of HTTP Trigger functions, my honest suggestion will to go and read this article HTTP Trigger Azure Function(Serverless Computing).
- Create a Service Library which will interact with Key Vault.
- Access the value from local.settings.json in our development environment.
- Create Azure Resources needed for this Demo.
- Provide Key Vault access identity to the Function app using the PowerShell command, manually from the portal.
Let's get started and create our Azure function using Visual Studio.
Select HTTP Trigger Template and select Azure Functions V1 because, in version V2, I had some issues with the HTTP trigger function when I tested on my local machine while writing this.

Template generated code for HTTP trigger.
- using System.IO;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Azure.WebJobs;
- using Microsoft.Azure.WebJobs.Extensions.Http;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Azure.WebJobs.Host;
- using Newtonsoft.Json;
- namespace AzFuncKeyVaultIntegration
- {
- public static class Function1
- {
- [FunctionName("Function1")]
- public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
- {
- log.Info("C# HTTP trigger function processed a request.");
- string name = req.Query["name"];
- string requestBody = new StreamReader(req.Body).ReadToEnd();
- dynamic data = JsonConvert.DeserializeObject(requestBody);
- name = name ?? data?.name;
- return name != null
- ? (ActionResult)new OkObjectResult($"Hello, {name}")
- : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
- }
- }
- }
Now, let's create a Service Library which will have all responsibility of calling Key Vault APIs and return us the actual value of our secret keys.
Creating a new Class Library from our solution


Install the below packages to interact with Key Vault from our services library from NuGet.
- Microsoft.Azure.Services.AppAuthentication Version:1.0.3
- Microsoft.Extensions.Configuration.AzureKeyVault Version: 2.2.0
Once done, create a class named KeyVaultService putting in the below code snippet.
- using Microsoft.Azure.KeyVault;
- using Microsoft.Azure.Services.AppAuthentication;
- using System;
- using System.Threading.Tasks;
- namespace Services
- {
- public class KeyVaultService
- {
- public async Task GetSecretValue(string keyName)
- {
- string secret = "";
- AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
- var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
- //slow without ConfigureAwait(false)
- //keyvault should be keyvault DNS Name
- var secretBundle = await keyVaultClient.GetSecretAsync(Environment.GetEnvironmentVariable("keyvault") + keyName).ConfigureAwait(false);
- secret = secretBundle.Value;
- Console.WriteLine(secret);
- return secret;
- }
- }
- }
Now, reference the service library in your Azure Function project.

So now, let's consider I have created two secrets in Azure Key Vault; for example:
- applicationSecret2
- secret2
Now I want to access the Key Vault secret applicationSecret2 with the help of managed identities and another secret, secret2, with the help of Key Vault references for Application Settings on Azure.












Андрій ШиряєвPosted Apr 21, 2021, 3:02 PM
microsoft.azure.key vault is deprecated