Bmoe
Bmoe

Reputation: 978

How to Override Web.config values with Azure KeyVault in .NET Framework 4.7.1

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

Answers (1)

kavya Saraboju
kavya Saraboju

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.

  • You need to provide a resource group, unique name and location ,then click on Review + Create.
  • Can refer Steps to create Azure keyvault here>>(https://learn.microsoft.com/en-us/azure/key-vault/quick-create-portal)
  • Next select the Secrets blade and add your app settings and connection strings that can be accessed in web.config file . You can click on the Generate/Import button and choose the Upload options as Manual. Then configure your app settings and connection strings - keys and values to the Name and Value options. And keep other options as default.

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. enter image description here

  • From the options, choose Secure Secrets with Azure Key Vault option.

enter image description here

  • Now you may need to sign in if not already signed in to your account and then select rquired key vault from the list. enter image description here

  • 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)

  • In Solution Explorer, right-click on your project, and select Manage NuGet Packages. In the Browse tab, locate and install Microsoft.Configuration.ConfigurationBuilders.Azure

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

Related Questions