【发布时间】:2021-06-25 10:52:09
【问题描述】:
尝试使用azure key vault 启用column encryption 时出现以下错误
无法从“方法组”转换为“TokenCredential”
我正在使用.Net Core 3.1
主类
static void Main(string[] args)
{
InitializeAzureKeyVaultProvider();
}
private static void InitializeAzureKeyVaultProvider()
{
_clientCredential = new ClientCredential(clientId, clientSecret);
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider =
new SqlColumnEncryptionAzureKeyVaultProvider(GetToken); // error comes here
Dictionary<string, SqlColumnEncryptionKeyStoreProvider> providers =
new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
providers.Add(SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider);
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
}
private static async Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, _clientCredential);
if (result == null)
throw new InvalidOperationException("Failed to obtain the access token");
return result.AccessToken;
}
我正在尝试使用key vault 来解密SQL Server 中的加密列值,我指的是这个文档:https://docs.microsoft.com/en-us/azure/azure-sql/database/always-encrypted-azure-key-vault-configure?tabs=azure-powershell
【问题讨论】:
-
您缺少括号来调用该方法,将
()添加到GetToken的末尾并传递正确的参数 -
该代码是为
Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider包的版本 1 编写的,但您已导入 v2 -
DavidG 是对的,您尝试调用的构造函数是 this one [...由于 Microsoft 文档链接过长]
-
你需要做类似
new SqlColumnEncryptionAzureKeyVaultProvider(new ClientCertificateCredential(tenantId, clientId, clientCertPath)) -
@DavidG 通过降级到 v1 错误得到解决。惊人的。让我进一步测试。
标签: c# sql-server encryption azure-keyvault always-encrypted