【问题标题】:Using always encrypted on a entity framework [code first] database在实体框架 [code first] 数据库上使用始终加密
【发布时间】:2017-10-30 16:40:44
【问题描述】:

我有一个首先使用实体​​框架/代码的 MVC 应用程序。我正在尝试设置始终加密以加密列(社会安全号码/ SSN)。我在 Azure 中运行一切,包括使用 Azure 保管库来存储密钥。

我有两个模型,SystemUser 和 Person。 SystemUser 本质上是一个可以管理 1 个或多个 People 的帐户/登录名。

定义有点像:

public class Person
{
        [StringLength(30)]
        [Column(TypeName = "varchar")]
        public string SSN { get; set; } // Social Security Number
        ...
        [Required, MaxLength(128)]
        public string SystemUserID { get; set; }
        [ForeignKey("SystemUserID")]
        public virtual SystemUser SystemUser { get; set; }
        ...
}

public class SystemUser
{
        ...
        [ForeignKey("SystemUserID")]
        public virtual HashSet<Person> People { get; set; }
        ...
}

我有一个非常基本的页面设置,它只是查找用户并打印出他们的 SSN。这行得通。然后我调整了页面以更新 SSN,这也有效。这对我来说意味着 Always Encrypted 配置和 Azure Vault 设置正确。我在连接字符串中有“Column Encryption Setting=Enabled”,我使用 SSMS 加密了列 SSN(我正在使用确定性)。

在我的 SystemUser 类中,我有以下方法作为 Identity 的实现:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<SystemUser> manager)
{
        ...
        if (this.People.Any())
        {
                ...
        }
        ...
}

这用于用户登录。运行代码会得到:

System.Data.Entity.Core.EntityCommandExecutionException:错误 执行命令定义时发生。见内 细节例外。 ---> System.Data.SqlClient.SqlException: 操作数类型冲突:varchar 与加密的 varchar(30) 不兼容 与 (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto11', column_encryption_key_database_name = 'xxx') collat​​ion_name = 'Latin1_General_BIN2'

“if (this.People.Any())”上面的行似乎失败了。在该行之前放置一个断点会显示有关 this.People 的以下内容:

'((System.Data.Entity.DynamicProxies.SystemUser_9F939A0933F4A8A3724213CF7A287258E76B1C6775B69BD1823C0D0DB6A88360)this).People' 抛出类型异常 'System.Data.Entity.Core.EntityCommandExecutionException' System.Collections.Generic.HashSet {System.Data.Entity.Core.EntityCommandExecutionException}

这里有什么想法吗?我是否在做 Always Encrypted 不支持的事情?

【问题讨论】:

    标签: entity-framework code-first identity dynamic-proxy always-encrypted


    【解决方案1】:

    实体框架始终不支持加密。 MS 仍在工作。

    【讨论】:

      【解决方案2】:

      此博客 Using Always Encrypted with Entity Framework 6 解释了如何使用 Always Encrypted with Entity Framework 6 for Database first 和 Code First From existing database 和 Code first-Migrations 以及针对不同场景和问题的解决方法。

      【讨论】:

        【解决方案3】:

        根据https://blogs.msdn.microsoft.com/sqlsecurity/2015/08/27/using-always-encrypted-with-entity-framework-6/

        将常量参数作为闭包传递——这将强制参数化,产生>正确的查询:

        var ssn = "123-45-6789";

        context.Patients.Where(p => p.SSN == ssn);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-03
          相关资源
          最近更新 更多