We have a ASP.NET MVC .NET 4.7.2 project that I want to migrate standard web.config settings (appSettings and ConnectionStrings) into Azure Key Vault.
Following these sites as reference,
https://peterbozso.com/2019/03/18/key-vault-asp-net.html
all seemed reasonably straight forward using configBuilders.
I first implemented just the standard Secrets file configBuilder, and all worked tickey boo.
Tring to get the Azure Config Builder working is where the issues began.
The main issue is getting Visual Studio for local debugging connected to the Key Vault. Documentation seemed to indicate adding Azure Key Vault as a Connected Service, which I have done.
So given the following in the web config
<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="AS_AzureKeyVault" prefix="AppSetting-" stripPrefix="true" vaultName="${KEY_VAULT_NAME}" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="CS_AzureKeyVault" prefix="ConnStr-" stripPrefix="true" vaultName="${KEY_VAULT_NAME}" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</builders>
</configBuilders>
<appSettings configBuilders="AS_AzureKeyVault">
<add key="KEY_VAULT_NAME" value="nameofvault" />
<!-- ... -->
</appSettings>
<connectionStrings configBuilders="CS_AzureKeyVault">
<add name="DBConn" connectionString="from key vault" />
<!-- ... -->
</connectionStrings>
Just throws error of
The configBuilder 'AS_AzureKeyVault' failed while processing the configuration section 'appSettings'.: Error in Configuration Builder 'AS_AzureKeyVault'::GetValue(KEY_VAULT_NAME)
with additional information on the stack trace of
MsalServiceException: AADSTS70002: The client does not exist or is not enabled for consumers.
and further down of
[AuthenticationFailedException: SharedTokenCacheCredential authentication failed.]
and
[AuthenticationFailedException: DefaultAzureCredential authentication failed.]
Which just seems to be an error connecting. From my understanding , the Visual Studio Connected Services uses my credentials from Visual Studio to connect to the Azure Key Vault. I have confirmed these credentials are correct, and the Connected Service dialog lists the key vault correctly with the correct subscription and account. I have no other Azure accounts on the machine (although I did have, and have since all deleted, thinking it may have been using the wrong account).
I additional tried connecting via AZ CLI, just in case it was trying those credentials, and confirmed connected via CLI with the correct user and Subscription, but still no luck through Visual Studio.
I am stumped. The reference website I used seem to make this trivial.
Packages and Versions involved
I created a new project in Visual Studio, ASP.NET MVC .NET Framework 4.7.2 Project, with no changes, was only trying to prove the concept of connecting to Azure Key Vault for local debugging.
<package id="Antlr" version="3.5.0.2" targetFramework="net472" />
<package id="Azure.Core" version="1.0.2" targetFramework="net472" />
<package id="Azure.Identity" version="1.1.1" targetFramework="net472" />
<package id="Azure.Security.KeyVault.Keys" version="4.0.0" targetFramework="net472" />
<package id="Azure.Security.KeyVault.Secrets" version="4.0.0" targetFramework="net472" />
<package id="bootstrap" version="3.4.1" targetFramework="net472" />
<package id="EntityFramework" version="6.4.4" targetFramework="net472" />
<package id="jQuery" version="3.4.1" targetFramework="net472" />
<package id="jQuery.Validation" version="1.17.0" targetFramework="net472" />
<package id="Microsoft.AspNet.Mvc" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net472" />
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.0.0" targetFramework="net472" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net472" />
<package id="Microsoft.Configuration.ConfigurationBuilders.Azure" version="2.0.0" targetFramework="net472" />
<package id="Microsoft.Configuration.ConfigurationBuilders.Base" version="2.0.0" targetFramework="net472" />
<package id="Microsoft.Configuration.ConfigurationBuilders.UserSecrets" version="2.0.0" targetFramework="net472" />
<package id="Microsoft.Identity.Client" version="4.1.0" targetFramework="net472" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.11" targetFramework="net472" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net472" />
<package id="Modernizr" version="2.8.3" targetFramework="net472" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net472" />
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
<package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net472" />
<package id="System.Security.Cryptography.ProtectedData" version="4.5.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="4.6.0" targetFramework="net472" />
<package id="System.Text.Json" version="4.6.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="WebGrease" version="1.6.0" targetFramework="net472" />