如何从未注册的应用程序(.net 框架 webapp)访问 azure key vault

How to access an azure keyvault from an non registeres app (.net framework webapp)

我正在尝试访问我的 azure keyvault 我已经从我的网络应用程序设置,由于遗留问题无法在 azure 中注册。

我现在已经通过连接服务“连接”了应用程序与密钥保管库,然后修改了 web.config 并安装了一堆 nuget 文件。

当我现在尝试通过

获取存储在我的天蓝色密钥库中的秘密时
   var secret = ConfigurationManager.AppSettings["ApiKey"];

我在 运行 站点

时似乎遇到了这个错误
 Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException: Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/311a22fd-9576-40ab-977e-a2f0a4d2cd36. Exception Message: Tried the following 4 methods to get an access token, but none of them worked.
Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/311a22fd-9576-40ab-977e-a2f0a4d2cd36. Exception Message: Tried to get token using Managed Service Identity. Unable to connect to the Managed Service Identity (MSI) endpoint. Please check that you are running on an Azure resource that has MSI setup.
Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/311a22fd-9576-40ab-977e-a2f0a4d2cd36. Exception Message: Tried to get token using Visual Studio. Access token could not be acquired. Visual Studio Token provider file not found at "C:\Users\local.temp.page\AppData\Local\.IdentityService\AzureServiceAuth\tokenprovider.json"
Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/311a22fd-9576-40ab-977e-a2f0a4d2cd36. Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. ERROR: Please run 'az login' to setup account.

Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/311a22fd-9576-40ab-977e-a2f0a4d2cd36. Exception Message: Tried to get token using Active Directory Integrated Authentication. Access token could not be acquired. unknown_user_type: Unknown User Type

它说的是正确的,我没有到密钥保管库的明确连接字符串 - 但是“连接”服务到密钥保管库应该没有处理这个问题吗? 并且在 git 中版本化连接字符串在存储密码方面不会违反直觉?

那么我如何访问我的连接服务,而不实际存储访问 Azure 密钥库的凭据?

我如何在一个解决方案中管理两个密钥保管库(一个用于开发环境,一个用于生产环境)?

how do i access my connected services, without actually storing the credentials of accessing the azure key vault?

  • 使用 Azure AD 托管服务标识 从所有环境访问 Key Vault,而无需在应用程序中存储任何凭据。
  • 托管身份在 Azure Active Directory .
  • 中为 Azure 服务提供自动托管身份
  • 它有助于对任何支持 AAD 身份验证的服务进行身份验证,而无需在您的代码中维护凭据。
  • 从安全角度来看,这是一个很棒的功能,因为您无法访问凭据。
  • 无需任何额外费用即可使用托管身份。

Refer steps to read a secret stored in an Azure Key Vault instance and Use a managed identity to connect Key Vault to an Azure web app in .NET

how do i manage two key vaults within one solution (one for dev env and one for prod env)?

请参阅 Development environment , Production environment and Production and Development environments

中的管理密钥保管库

更多信息请参考this

您有一个 .net Framework 应用程序,它是 运行 内部部署的。您需要来自 KeyVault 的一些秘密可用于您的应用程序,但无法在需要时从您的应用程序代码中获取它。您也不想将实际秘密放入 git 中的 web.config(这很好)。你还有多环境的问题。

鉴于所有这些,我首先建议您不要在 web.config 中添加连接字符串,而只需为其添加一个 令牌 。这完全可以放入您的 Git 存储库。

<configuration>
  <appSettings>
    <add key="ApiKey" value="API_KEY" />

然后您像今天一样构建您的应用程序,生成包含 web.config 文件的发布文件夹。您可以将此称为您的 构建阶段

在 运行 实际 部署 到 IIS 之前,您需要用 KeyVault 中的实际值替换令牌。为此,您的部署过程将需要访问 KeyVault(但不是您的应用程序)。您执行此操作的方式取决于您部署应用程序的方式。它可能只是一个获取机密并修改 web.config 的 Powershell 脚本。如果你使用 Azure Pipelines,你可以 link variables directly to KeyVault。如果您部署到生产环境,您只需从不同的 KeyVault 获取机密。这也确保您可以在多个环境中使用相同的构建工件,只是配置不同。