使用 Entity Framework Core 的数据保护

ten*_*ten 6 asp.net-core-webapi asp.net-core-2.2 ef-core-2.2

所以我遵循微软的官方指南 ( 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>();
Run Code Online (Sandbox Code Playgroud)

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>();

Run Code Online (Sandbox Code Playgroud)

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

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

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

有什么线索吗?

ten*_*ten 6

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

.SetApplicationName("myapp")
Run Code Online (Sandbox Code Playgroud)

我将证书的路径更改为:

.ProtectKeysWithCertificate(new X509Certificate2(Path.Combine(_hostingEnvironment.ContentRootPath,"wibit-test-cert.pfx"), "password"))
Run Code Online (Sandbox Code Playgroud)

也可能是权限问题,因为当我在 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>();
Run Code Online (Sandbox Code Playgroud)

  • 你的方法不好。您应该在 Windows 或任何操作系统中安装证书,并使用指纹来查找存储中的证书。.net 很容易反编译,因此您的密码是可读的纯文本。另外,许多人在安装后会遇到问题,因为您还需要授予 IIS_USRS 权限才能读取证书。(右键单击证书(在 certlm 应用程序中)管理私钥...) (3认同)