如何在应用程序中使用 Azure KeyVault 参考 connectionString

How to use Azure KeyVault reference for connectionString in an App

更新:修复在我下面的评论中。主持人能否将其固定为答案。谢谢!

我试图在不对 Web.Config 文件进行任何代码更改的情况下设置 Azure KeyVault。我有一个 Web 应用程序,它有一个用于数据存储的 SQL 数据库,我正在尝试以这种方式替换 connectionString,以便在使用 Azure 应用程序服务配置部署应用程序期间动态注入它。我正在使用对我的密钥保管库机密的引用来在我的应用程序服务配置中引用它。但是,我无法访问数据,并且出现以下错误:

https://imgur.com/a/9MkAfF5

我试过以下方法:

  1. 创建 KeyVault 实例
  2. 向 KeyVault 添加机密
  3. 授予对 Web 应用程序的访问权限,以便能够使用 Azure Active Directory 访问此 KeyVault。我创建了一个新的应用程序注册,分配了密钥保管库的访问策略,为该活动目录提供了能够 access/set/get/list 机密的所有权限。
  4. 通过转到我的应用程序服务并进行配置并添加连接字符串,将 Key Vault 集成到我的 Azure 应用程序服务中,如下所示:

https://imgur.com/a/kBKKbTG

如您所见,我使用的是来自 KeyVault 的引用,格式如下:

@Microsoft.KeyVault(SecretUri=https://xxx.vault.azure.net/secrets/mysecret/xxxzxxzxxzxzx)

我什至试过使用这样的参考:

@Microsoft.KeyVault(VaultName=xxx-kv-arm;SecretName=connectionString;SecretVersion=xxx)

在我的 Azure 应用服务配置中从密钥保管库访问机密并在部署期间将其作为 connectionString 传递。

我的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" />
  </configSections>
  <connectionStrings>
    <add name="SchoolContext" connectionString=" " providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <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>

</configuration>

我到底做错了什么?

However, I do not see this app service in my azure active directory, any clues as to why this is?

您可以在企业应用程序下找到应用程序,记得将应用程序类型设置为所有应用程序

问题已由操作员解决,只需将其添加到答案中即可让其他人看到。

转到应用程序服务,转到身份,然后启用系统分配的身份。这将允许我们转到我的 Key Vault 实例并为此应用服务本身而不是新的 Azure Active Directory 配置访问策略。

这里是document供您参考。