当前位置: 首页 > 知识库问答 >
问题:

在ASP.NET应用程序中配置Azure KeyVault的问题

拓拔浩阔
2023-03-14

我试图使用Azure KeyVault来获取作为秘密存储在KeyVault中的ConnectionString。我试图在ASP.NET MVC Web应用程序中实现这一点,而不是在ASP.NET核心中。目前,我无法这样做,错误是:

(https://imgur.com/lwhovga)

我一直在遵循以下指南,在ASP.NET中使用KeyVault来屏蔽connectionStrings:

<?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>

共有1个答案

郑星辰
2023-03-14

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

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

一切都会好起来的!你可以在官方文档中找到更多关于这方面的信息/例子。

 类似资料:
  • org.springframework.beans.factory.XML.xmlBeanDefinitionStoreException:ServletContext资源[/web-inf/servlet-context.XML]的XML文档中的第38行无效;嵌套异常是org.xml.sax.SAXParseException;行号:38;列号:109;CVC-complex-type2.4.a

  • ASP.NETCore支持一个新的配置系统,如下所示:https://docs.asp.net/en/latest/fundamentals/configuration.html 也支持这种模式吗?NET核心控制台应用程序? 如果不是,什么是替代以前的和模型?

  • 我尝试了一个显示其他响应的测试用例,我将一步一步地解释它: > 我在我的设备上安装了MDM配置文件,这提示我安装了一个应用程序,并且我成功地安装了它。应用程序已进入“应用程序中的配置文件”部分。 然后我手动删除了应用程序。

  • 我试图在一个sails应用程序中正确地创建一个angular2应用程序。 我希望AngularApp在 /management网址下运行 我所做的: 创建ManagementController。js: 在“视图/角度/角度开始”下创建视图。ejs 正在加载... 创建了一个文件夹“资产/管理”,我的angular应用程序将位于该文件夹中 已创建资产/管理/应用程序文件夹 添加到副本中。js: g

  • 我正在尝试在我的Jersey/Hk2应用程序中注入EntityManagerFactory(作为一个单例)和EntityManager(在请求范围中)。 My EntityManagerFactoryProvider(单例): My EntityManagerProvider(每个请求)为: 我的应用程序是: 和web.xml 多谢

  • 你可以查看或更改 Navicat Monitor 的应用程序设置,例如端口、网站网址和 IP 地址。若要配置应用程序设置,请前往“配置”->“应用程序设置”。 这里列出了 Navicat Monitor 的应用程序设置。你可以编辑以下设置: 端口 Navicat Monitor 将侦听的端口号。 网站网址 将用于警报电子邮件内的 Navicat Monitor 网站网址。 IP 地址 如果机器已被