Exercise 3: Migrating to Azure Key Vault
In this exercise, attendees will learn how to migrate web application to utilize Azure Key Vault rather than storing valuable credentials (such as connection strings) in application configuration files.
- From the extracted GitHub directory, open the Hands-on labWebAppInsuranceAPI_KeyVaultInsuranceAPI.slnsolution.
Note: Be sure you open the correct solution (there are two).
- Switch to your Azure Portal.
- Select Key Vaults, then select your Azure Key Vault.
- Select Secrets, then select +Generate/Import.
- For the Upload Options, select Manual.
- For the Name, enter InsuranceAPI.
- For the Value, copy the connection string information from the InsuranceAPI solution web.config file in Exercise 2.
- Select Create.
- Select Secrets.
- Select InsuranceAPI.
- Select the current version.
- Copy and record the secret identifier URL for later use:
- In the Azure Portal, select Azure Active Directory, then select App Registrations.
- Select + New application registration.
- For the name, type AzureKeyVaultTest.
- For the Sign-on URL, type http://localhost:12345.
- Select Create.
- Select the new AzureKeyVaultTest application.
- Copy and record the Application ID for later use.
- Select Settings.
- Select Keys.
- For the description, enter InsuranceAPI.
- For the Expires, select In 1 year.
- Select Save.
- Copy and record the key value for later use.
- Switch back to Azure Portal and select your Azure Key Vault.
- Select Access Policies.
- Select + Add New.
- Select Select principal, type AzureKeyVaultTest.
- Select the application service principal, select Select.
- Select the Secret permissions drop-down, check the Get and List permissions.
- Select OK.
- Select Save.
- Switch to Visual Studio.
- In the menu, select View->Other Windows->Package Manager Console.
- In the new window that opens, run the following commands:a. Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatformb. Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.16.204221202c. Install-Package Microsoft.Azure.KeyVault
Note: These already exist in the project but are provided as a reference.
- From Solution Explorer, double-click the web.config file to open it.Notice the appSettings section has some token values:
- Replace the ClientId and ClientSecret with the values from Task 2.
- Replace the SecretUri with the Azure Key Vault secret key Uri from Task 1.
- Save the file.
- Open the web.config, and comment out or delete the connectionString from the file at line 78.
- Open the global.asax.cs file, and place a break point at line 28.
Note: This code makes a call to get an accessToken as the application you set up above, then make a call to the Azure Key Vault using that accessToken.
- Run the solution, and press F5.You should see that you execute a call to Azure Key Vault and get back the secret (which in this case is the connection string to the Azure Database).
- Press F5, and navigate to http://localhost:portno/api/Users, you should get an error. Because you encrypted the column in the previous exercise, EntityFramework is not able to retrieve the value. You would need to add the AzureKeyVaultProvider for Entity Framework reference to the project and then register the provider code in order for .NET to handle the encrypted column and add the “Column Encryption Setting=Enabled” to the connection string.