我已将项目(WebApi .net 4.7)KeyVault 添加为连接服务。到目前为止一切顺利,它只是在 web.config 中硬编码了 KeyVault 名称。然后,我添加了
Microsoft.Configuration.ConfigurationBuilders.Environment
到项目,从本地环境中读取 KeyVaultName。现在 web.config 看起来像这样:
<configBuilders>
<builders>
<add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="AzureKeyVault" vaultName="keyvault-name" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=1.0.0.0, Culture=neutral" /></builders>
</configBuilders>
问题是,升级Microsoft.Configuration.ConfigurationBuilders.Azure
到 2.0 Visual Code 后似乎无法连接到 azure KeyVault。我正在关注本指南https://peterbozso.com/2019/03/18/key-vault-asp-net.html我错过了什么吗?否则此版本将无法在本地环境中使用。我猜在 2.0 中使用包 ConfigBuilder.Azure 部署 api 时会起作用,只是我们希望它也能在本地工作。
我们需要在 2.0 版中使用它,因为它允许从环境中读取变量(https://github.com/aspnet/MicrosoftConfigurationBuilders#appsettings-parameters)
这是将 ConfigBuilder.Azure 升级到 2.0 后的 web.config。
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<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="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="AzureKeyVault" vaultName="keyvault-name" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</builders>
</configBuilders>
<appSettings configBuilders="Environment, AzureKeyVault">
<add key="someKey" value="seomValue" />
</appSettings>
我在想这是我使用 VS2017 的问题,现在切换到 VS2019 没有任何区别。