Of the three different ways to access an azure key vault from an ASP.NET core application, if your app runs on an azure resource, the best option is using azure managed identities for simplicity and the highest security. In this post, I go over how I configure the application and azure sides to leverage azure managed identities when accessing the key vault.
The ASP.NET core application authenticates with Azure AD services to obtain an access token to access the key vault. With managed identities, we don’t have to provide the app’s credentials, only the URL to the key vault. For example, I have the following in the appsettings.json file for one of my applications that runs on an azure VM.
{ "KeyVault": { "URL": "https://myvaultname.vault.usgovcloudapi.net/" } }
The URL is all I need. From the document,
The app is deployed to Azure, and Azure authenticates the app to access Azure Key Vault only using the vault name stored in the appsettings.json file.
Use Managed identities for Azure resources
In the StartUp file, I use the Microsoft.Azure.Services.AppAuthentication library to handle the authentication.
// use Identity Management var azureServiceTokenProvider = new AzureServiceTokenProvider(); var keyVaultClient = new KeyVaultClient( new KeyVaultClient.AuthenticationCallback( azureServiceTokenProvider.KeyVaultTokenCallback)); builder.AddAzureKeyVault(keyVaultOptions.URL, keyVaultClient, new DefaultKeyVaultSecretManager());
The AzureServiceTokenProvider
constructor optionally accepts a connection string as a parameter. When no connection string is given, as shown in the above snippets, the library uses different methods such as managed identity to obtain an access token.
// // Summary: // Creates an instance of the AzureServiceTokenProvider class. If no connection // string is specified, Managed Service Identity, Visual Studio, Azure CLI, and // Integrated Windows Authentication are tried to get a token. Even If no connection // string is specified in code, one can be specified in the AzureServicesAuthConnectionString // environment variable. // // Parameters: // connectionString: // Connection string to specify which option to use to get the token. // // azureAdInstance: // Specify a value for clouds other than the Public Cloud. public AzureServiceTokenProvider(string connectionString = null, string azureAdInstance = "https://login.microsoftonline.com/");
On Azure, I just need to do two simple steps to leverage azure managed identities:
It’s straightforward to turn on Identity for the resource. In azure portal, just navigate to your resource configuration pages, go to Identity under Settings. You can use either System assigned identity or create your own (User assigned) and assign to the resource. Using a user assigned identity is out of the scope for this post. If you want to learn more, I have found this post to be helpful. For me, I use system assigned identity. I simply enable system assigned identity to the azure VM on which my app runs by just setting the Status to On.
In the key vault, I just need to grant access to the azure VM via Access policies. I can search for the azure VM using its identity. Depending on your need, you may grant different permissions. For just accessing secrets in the vault, I find it is necessary to grant both the List and Get permissions under Secret Permissions.
What is managed identities for Azure resources?
Use managed identities for azure resources
Microsoft.Azure.Services.AppAuthentication nuget
Using User Assigned Managed Identity to Access Azure Key Vault from Azure App Service
Building a fully multitenant system using Microsoft Identity Framework and SQL Row Level Security
Enhancing ASP.NET Core/Blazor App Security and Reusability with HttpMessageHandler and Named HttpClient
Authenticate against azure ad using certificate in a client credentials flow
How to retrieve connection strings in azure key vault from ASP.NET using configuration builders, XML transformation and azure devops.
Securely log to blob storage using NLog with connection string in key vault.
Access azure key vault from an ASP.NET core app on IIS using X.509 certificate
Three ways of authenticating a Windows virtual machine against Azure Key Vault.
Secure app settings in ASP.NET Core 2