【问题标题】:Data Protection using Entity Framework Core使用 Entity Framework Core 进行数据保护
【发布时间】:2019-10-25 10:42:15
【问题描述】:

所以我按照微软的官方指南 (https://docs.microsoft.com/el-gr/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-2.2&tabs=visual-studio) 使用 Entity Framework Core 加密数据并将它们存储在数据库中,但我无法让它在多台机器上工作。所以我使用了 Entity Framework Core 实现,因为在指南中说“使用这个包,可以在 Web 应用程序的多个实例之间共享密钥。”。从部署版本(例如 xyz.com)中使用该应用程序时,该应用程序运行良好,但它不允许我从 localhost 干扰。之后当我的虚拟机被最大化并且我想添加另一个时会出现问题吗?如果是这样,我怎样才能让它在部署的站点和不同的机器上工作?没有实现它的教程,我到处搜索。非常感谢。

services.AddDataProtection()
                .UseCryptographicAlgorithms(
                    new AuthenticatedEncryptorConfiguration()
                    {
                        EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                        ValidationAlgorithm = ValidationAlgorithm.HMACSHA256,

                    }
                ).PersistKeysToDbContext<DataContext>();

2019 年 12 月 6 日更新

所以我遵循了微软的文档 (https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-encryption-at-rest?view=aspnetcore-2.2) 它指出:

“如果应用程序分布在多台机器上,那么在机器之间分发共享的 X.509 证书并配置托管应用程序以使用该证书对静态密钥进行加密可能会很方便”

我使用本教程生成了 x.509 证书:

(https://www.youtube.com/watch?v=1xtBkukWiek)

我更新的代码:

        services.AddDataProtection()
                .UseCryptographicAlgorithms(
                    new AuthenticatedEncryptorConfiguration()
                    {
                        EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                        ValidationAlgorithm = ValidationAlgorithm.HMACSHA256,

                    }
                )
                // )
                .ProtectKeysWithCertificate(new X509Certificate2("wibit-test-cert.pfx", "password"))
                .PersistKeysToDbContext<DataContext>();

在我的本地机器上测试时它工作正常,但是当我部署它时,我得到了这个错误:

错误:“系统找不到指定的文件”

我尝试了几种方法来修复它,包括 _hostingEnvironment.ContentRootPath 或 WebRootPath。这两种方法和我在更新代码中使用的方法都可以在我的机器上工作,但不能在部署的应用程序中工作。

有什么线索吗?

【问题讨论】:

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


    【解决方案1】:

    我终于修好了! 问题是我没有设置应用名称:

    .SetApplicationName("myapp")
    

    我将证书的路径更改为:

    .ProtectKeysWithCertificate(new X509Certificate2(Path.Combine(_hostingEnvironment.ContentRootPath,"wibit-test-cert.pfx"), "password"))
    

    这也可能是权限问题,因为当我在 A2Hosting 中托管应用程序时,它找不到指定的文件(wibit-test-cert.pfx),但是当我在 GCP Cloud 中部署时它可以工作!

    现在我可以使用同一个数据库和不同的应用来加密和解密数据。

    所以我的最终代码是这样的:

    services.AddDataProtection()
      .UseCryptographicAlgorithms(
          new AuthenticatedEncryptorConfiguration()
             {
                 EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
                 ValidationAlgorithm = ValidationAlgorithm.HMACSHA256,
    
              }
           )
           .SetApplicationName("myapp")
           .ProtectKeysWithCertificate(new X509Certificate2(Path.Combine(_hostingEnvironment.ContentRootPath,"wibit-test-cert.pfx"), "password"))
           .PersistKeysToDbContext<DataContext>();
    

    【讨论】:

    • 你的方法不好。您应该在 Windows 或任何操作系统中安装证书,并使用指纹代替在商店中查找证书。 .net 很容易反编译,因此您的密码是纯文本可读的。安装后也有很多人遇到问题,因为您还需要授予 IIS_USRS 权限才能读取证书。 (右键单击证书(在 certlm 应用程序中)管理私钥...)
    猜你喜欢
    • 2017-02-15
    • 2019-07-06
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2021-12-21
    相关资源
    最近更新 更多