c# - 如何从未注册的应用程序(.net Framework Web应用程序)访问Azure Key Vault

标签 c# azure azure-keyvault .net-4.8

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

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

当我现在尝试通过以下方式获取我存储在我的 azure keystore 中的 secret 时

   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

它所说的是正确的,我没有到 key 保管库的显式连接字符串 - 但是将服务“连接”到 key 保管库是否应该处理这个问题? 并且在 git 中对连接字符串进行版本控制对于存储密码来说不是违反直觉的吗?

那么,如何在不实际存储访问 Azure Key Vault 的凭据的情况下访问我的连接服务?

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

最佳答案

您有一个在本地运行的 .net 框架应用程序。您需要 KeyVault 中的一些 secret 可供您的应用程序使用,但在需要时无法从您的应用程序代码中获取它。您也不想将实际的 secret 放入 git 中的 web.config 中(这很好)。您还面临多个环境的问题。

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

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

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

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

关于c# - 如何从未注册的应用程序(.net Framework Web应用程序)访问Azure Key Vault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71095915/

相关文章:

c# - 默认构造函数和无参数构造函数之间的区别?

c# - 如何使用户控件像窗口一样在屏幕上可拖动

azure - Function App 每分钟仅使用来自启用 session 主题的服务总线 8 条消息

Azure Vault 和 VM 磁盘加密 'Access Denied'

文本框 : float 的 C# 输入验证

c# - 无法使用 IP 地址连接到应用程序

azure - AAD 和 MSI 之间的区别

azure - 如何从 Startup.cs 中访问 KeyVault Secret 值(适用于 Serilog SQL Sink)

oracle - 如何使用 SQL 查询查找 Oracle 数据库的 SID

azure - 使用 Azure 数据工厂进行 API 调用时出现错误 500