Reputation: 978
How can I implement Microsoft's Azure KeyVault in a ASP.NET Framework 4.7.1 WebForms application to override values in web.config
with values from KeyVault? I do see references that we need a minimum version of .NET Framework 4.7.1 in order to do it but the examples Microsoft provides are for .NET Core. I have my configs as web.config
files instead of appsettings.json
. I also have Global.asax.cs
files instead of Startup.cs
and Program.cs
.
Upvotes: 13
Views: 15592
Reputation: 10831
To implement Microsoft's Azure KeyVault in a ASP.NET Framework 4.7.1 WebForms application , first you need to create an Azure Key Vault.
Configuration builders in ASP.NET provide a way to modify and/or override the values coming from your configuration files (Web.config in the case of ASP.NET) by using different sources (environment variables, Key Vault, etc.).
Connecting to Azure Key Vault:
To connect to Azure Key Vault from Visual Studio, you need to right click on the project and select Add > Connected Service menu.
From the options, choose Secure Secrets with Azure Key Vault option.
Now you may need to sign in if not already signed in to your account and then select rquired key vault from the list.
And click on the Add button to add key vault reference to your application. This will add reference of the NuGet package Microsoft.Configuration.ConfigurationBuilders.Azure to the project.
Also it will add some configuration in the Web.Config file.
(OR)
Open your web.config file, and write the following code:
a) Add configSections and configBuilders as below with your keyvault name
<configSections>
<section
name="configBuilders"
type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
restartOnExternalChanges="false"
requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add
name="AzureKeyVault"
vaultName="vaultname"
type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=1.0.0.0, Culture=neutral"
vaultUri="https://vaultname.vault.azure.net" />
</builders>
</configBuilders>
b) Find the appSettings tag, add an attribute configBuilders="AzureKeyVault", and add a line as below:
<appSettings configBuilders="AzureKeyVault">
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="TextAnalyticsKey" value="from key vault" />
</appSettings>
<connectionStrings configBuilders="AzureKeyVault">
<add name="DefaultConnection" connectionString="from key vault" providerName="System.Data.SqlClient" />
<add key="StorageConnectionString" value="from key vault" />
</connectionStrings>
c) Edit the About method in HomeController.cs, to display the value for confirmation.
public ActionResult About()
{
ViewBag.Message = "Key vault value = " + ConfigurationManager.AppSettings["TextAnalyticsKey"];
}
This way you can connect and use Azure Key Vault in your classic ASP.NET MVC applications,if you’re application running is using .NET Framework 4.7 or later versions.
You can refer following documents for the detailed explaination of the same :
Upvotes: 9