无法使用密钥库提供程序解密列加密密钥:'AZURE_KEY_VAULT'

Failed to decrypt a column encryption key using key store provider: 'AZURE_KEY_VAULT'

我已将 Always encrypted 用于 Azure 数据库上的数据库 table 列。我已经阅读了这篇文章 - https://docs.microsoft.com/en-us/azure/sql-database/sql-database-always-encrypted-azure-key-vault - 非常仔细地阅读了它。我已经加密了这些列,并且成功完成了。我可以看到该列已加密。

当我尝试按照同一篇文章尝试查看来自应用程序的数据时,我收到错误消息:

Failed to decrypt a column encryption key using key store provider: 'AZURE_KEY_VAULT'. Verify the properties of the column encryption key and its column master key in your database. The last 10 bytes of the encrypted column encryption key are: '54-36-01-E0-5C-A1-82-80-B1-B4'.

这是代码中的错误,但在我消除该错误后,我得到:

Access denied. Caller was not found on any access policy. Caller: appid=afd26169-bbac-4a45-ad3c-2b4492d19c6e;oid=dabbc750-5601-442b-9809-3a17f74d5aa2;numgroups=0;iss=https://sts.windows.net/bd8eb048-c497-4576-80eb-99e763b83ffd/ Vault: AWKeyVault2;location=eastus2

值得注意的是,当我使用SSMS时,我将连接字符串选项设置为Column Encryption Setting=Enabled,我可以在SSMS中看到解密后的数据。

在大多数情况下,我认为这是一个权限问题,但我不知所措。我已授予 App Registration 对 Azure 订阅的完全访问权限。我还为应用程序注册生成了一个 'Secret',并在应用程序中使用它。如果我能看到来自 SSMS 的数据,我认为这意味着加密成功,所以这一定与应用程序无法获取 Azure Key Vault 中的密钥有关。我错过了什么?

好的,明白了。这绝对是一个权限问题。为了完全验证您是否拥有正确的权限,请转到 Key Vault Blade:

  • Select 从左侧的 Key Vault 资源边栏选项卡菜单访问策略
  • 单击顶部的 "add new" link/button
  • Select select 您正在使用的应用程序的主体(即您从中获得客户端 ID 的应用程序注册)
  • 确保从“密钥权限”下拉列表中授予它 "Decrypt"、"Sign"、"Get"、"UnwrapKey" 权限
  • 确保保存更改

这是最后一件事 - 您仍然必须确保您的应用注册具有正确的订阅权限。