【发布时间】:2018-12-18 21:07:01
【问题描述】:
我已使用 AzureKeyVault 加密 SQL 中的一些社会保障号列。这些列定义是 varchar(11) NULL。
我的代码中的模型具有以下属性:
[StringLength(11)]
[Column(TypeName = "varchar")]
[RegularExpression(RegExValidators.SSNRegex, ErrorMessage = "SSN must be a number")]
public string SSN { get; set; }
但是,我偶尔会在我的数据库日志中看到此错误:
System.Data.SqlClient.SqlException: Operand type clash: varchar is incompatible with varchar(11) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto2', column_encryption_key_database_name = 'DB NAME') collation_name = 'Latin1_General_BIN2'
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
奇怪的是,这种情况并不总是发生......只是每隔一段时间。我在 Global.asax Application_Start() 函数中运行此代码:
public static class AzureKeyVaultInit
{
private static string _clientId = ConfigurationManager.AppSettings["AzureKeyVaultAppClientId"];
private static string _clientSecret = ConfigurationManager.AppSettings["AzureKeyVaultAppSecret"];
private static ClientCredential _clientCredential;
private static bool _isInitialized = false;
private static readonly object _isInitializedLock = new object();
public static void InitializeAzureKeyVaultProvider()
{
if (string.IsNullOrEmpty(_clientId)) return;
lock (_isInitializedLock)
{
if (!_isInitialized)
{
_clientCredential = new ClientCredential(_clientId, _clientSecret);
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(GetToken);
Dictionary<string, SqlColumnEncryptionKeyStoreProvider> providers = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
providers.Add(SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider);
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
_isInitialized = true;
Core.Log.Info($"Initialized Azure Key Vault");
}
}
}
有什么明显的原因为什么我每隔一段时间就会收到这个错误?
【问题讨论】:
标签: entity-framework azure azure-web-app-service azure-keyvault always-encrypted