在.netcore 2中查询加密的数据

时间:2019-06-14 08:37:05

标签: asp.net-core-webapi asp.net-core-2.2

我遵循了Microsoft加密和解密数据的官方指南。我的问题是如何在数据库的数据中搜索给定的属性。例如,我想查询数据库中FirstName包含我输入的搜索名称的所有数据。但是FirstName在数据库中已加密,因此它具有以下值:“ CfDJ8GJ0pjnzz6BGph-AUfSYepqLrRxw5zqtqoh540M8wHqnnfZRuH542PMzLClloeYoQAq69kPRmUHnNdfg7J9jHc9ieIe1VQ9IZ_1WQ9Z1_W1Q1Z1C_1WQ9Z

检索到数据后,我可以成功保护和取消保护数据,但是我不能随时查询数据。为了解密数据,我使用了_protector.Unprotect(),但是当我将其放入where()时,它什么也不做,也没有收到任何错误。上面的代码由于某些原因无法正常工作。

var customers = await _context.Customers
            .Include(x => x.CustomerInformation)
            .Include(x => x.CustomerContact)
            .Where(x => _protector.Unprotect(x.CustomerInformation.FirstName).Contains(name))
            .ToListAsync();         

1 个答案:

答案 0 :(得分:0)

我修复了! 用大写字母创建名字时出现问题。所以我更新的用于查询加密数据的代码是这样的:

var customers = await _context.Customers
    .Include(x => x.CustomerInformation)
    .Include(x => x.CustomerContact)
    Where(x => 
            (_protector.Unprotect(x.CustomerInformation.FirstName).ToLower().Contains(name)) ||
            (_protector.Unprotect(x.CustomerInformation.LastName).ToLower().Contains(name)) ||
            (x.CustomerInformation.Code.ToLower().Contains(name))
         )
         .ToListAsync();