C# 在ASP.net应用程序中配置Azure KeyVault时出现问题

C# 在ASP.net应用程序中配置Azure KeyVault时出现问题,c#,asp.net,.net,azure,azure-keyvault,C#,Asp.net,.net,Azure,Azure Keyvault,我正在尝试使用Azure KeyVault获取已作为密钥库中的机密存储的连接字符串。我正在尝试在ASP.NETMVCWeb应用程序中而不是在ASP.NETCore中执行此操作。目前,我无法执行此操作,错误为: () 我一直遵循此指南使用KeyVault在ASP.net中屏蔽连接字符串: 我做了以下工作: -创建了一个密钥库并将秘密存储在其中。 -已发布我的MVC ASP.net Web应用程序。 -授予MVC ASP.net Web应用权限,使其能够使用访问策略从KeyVault访问机密。 -

我正在尝试使用Azure KeyVault获取已作为密钥库中的机密存储的连接字符串。我正在尝试在ASP.NETMVCWeb应用程序中而不是在ASP.NETCore中执行此操作。目前,我无法执行此操作,错误为:

()

我一直遵循此指南使用KeyVault在ASP.net中屏蔽连接字符串:

我做了以下工作: -创建了一个密钥库并将秘密存储在其中。 -已发布我的MVC ASP.net Web应用程序。 -授予MVC ASP.net Web应用权限,使其能够使用访问策略从KeyVault访问机密。 -在我的ASP.net MVC项目中为KeyVault添加了连接服务。 -成功添加后,我删除了已连接服务添加的vault URI属性,并确保vault名称正确无误。 -我将Web.Config中的appSettings标记替换为:

然而: 1.我发布了该应用程序,不再出现该错误。但是,我不太确定应用程序是否真的从KeyVault获取了connetionString

这是Web.Config文件中的代码:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <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="AzureKeyVault" vaultName="kv-TEST" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=1.0.0.0, Culture=neutral"  />
    </builders>
  </configBuilders>
  <!--<connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>-->
  <!--Azure connection string-->
  <connectionStrings>
    <add name="SchoolContext" connectionString="" providerName="System.Data.SqlClient" />

   <!-- <add name="ContosoUniversityLogin" connectionString="" providerName="System.Data.EntityClient" />
   -->
  </connectionStrings>
  <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" />
  </appSettings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.7.2" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.5" />
    <httpModules />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <entityFramework>
    <!--<contexts>
      <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
        <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />
      </context>
    </contexts>-->
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

如果您只想验证它是否来自Azure key vault,则只需从web.config中删除连接字符串,然后查看它是否有效

此外,您还可以尝试访问控制器中的密钥库密钥,并查看其是否获得所需的结果。 您可以简单地更新Azure key vault key-value对,并可以打印以理解。此外,如果您可以共享,代码将更新


希望有帮助。

看看您在问题中给出的示例配置,我认为问题在于您没有将配置生成器实际应用到ConnectionString部分。您需要执行与appSettings相同的操作,如下所示:

  <connectionStrings configBuilders="AzureKeyVault">
    <add name="SchoolContext" connectionString="" providerName="System.Data.SqlClient" />
  </connectionStrings>


您想让我们如何为您验证?我想帮助您自己验证。只是任何线索,但下面的用户帮助!ThanksI注释掉了ConnectionString,这导致我无法从数据库中检索任何数据。我认为之所以会发生这种情况,是因为AzureKeyVault提供的外部配置过度写入了ConnectionString部分。因此,我刚刚将connectionString部分更改为“”,这产生了相同的错误,这使我相信我没有正确地从KeyVault检索connectionString。有什么想法吗?更新了代码!希望这能让任何想帮忙的人更容易