【问题标题】:asp.net core data protection key encryptasp.net 核心数据保护密钥加密
【发布时间】:2021-01-06 12:29:45
【问题描述】:

在此链接中: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-5.0#data-protection

它说“如果未配置数据保护,则密钥将保存在内存中,并在应用重新启动时丢弃。”,我不希望这种情况发生,所以我配置了数据startup.cs 中的保护:

 services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo(@"PATH-HERE"))

当我启动应用程序对其进行测试时,日志中会显示一条警告:No XML encryptor configured. Key {GUID} may be persisted to storage in unencrypted form.

我发现我需要使用ProtectKeysWith* 来加密密钥。但是因为我正在尝试将应用程序发布到 Linux 服务器,所以我不能使用 ProtectKeysWithDpapiProtectKeysWithDpapiNG(因为它们只能在 Windows 服务器上使用),所以剩下的唯一选择是 X.509

基本上,我做了一些搜索,发现我可以使用这些命令来创建一个自签名的X.509 证书:

"C:\Program Files\Git\usr\bin\openssl.exe" genrsa -out private.key 2048

"C:\Program Files\Git\usr\bin\openssl.exe" req -new -x509 -key private.key -out publickey.cer -days 2000

"C:\Program Files\Git\usr\bin\openssl.exe" pkcs12 -export -out idp.pfx -inkey private.key -in publickey.cer

我可以像这样在启动时添加这个证书:

services
   .AddDataProtection()
   .PersistKeysToFileSystem(new DirectoryInfo(@"PATH-TO-SAVE-KEYS"))
   .SetDefaultKeyLifetime(new TimeSpan(90, 0, 0, 0, 0))
   .SetApplicationName("APPNAME-HERE")
   .ProtectKeysWithCertificate(new X509Certificate2(@"CERTIFICATE-PATH", "CERTIFICATE-PASSWORD"));

所以我的问题是我什至需要加密密钥吗?如果我应该,我的解决方案是否有效?我可以在生产中毫无问题地使用这个解决方案吗? (请记住,我将为我的应用程序使用 Linux 服务器)

更新 1: 我在 StackOverflow 问题中做了更多的挖掘,我发现了这个: https://stackoverflow.com/a/48867984/14951696.

显然,只要您在内部使用它,就可以使用自签名证书(就像我正在做的那样)。我会在发布我的应用后再次更新,以防有人有同样的问题。

更新 2: 我决定使用 Windows 服务器,我发现使用自签名证书加密密钥没有问题。有什么事我会再更新的。

【问题讨论】:

    标签: asp.net-core data-protection


    【解决方案1】:

    我的两分钱,不知道你发现的问题是什么,如果不是你可以我们下面的解决方案。

    为了有一个共享的数据保护密钥,需要明确地强制执行它。 需要注意的是,在启动时密钥在源中不存在,并与过期相关联。想法是将 XElement 及其名称保存在可用于在启动时检索该值的存储中。

    启动时:

     services.Configure<KeyManagementOptions>(options =>
            {
                IDataProtectionRepo dataProtection = services.BuildServiceProvider().GetRequiredService<IDataProtectionRepo>();
                options.NewKeyLifetime = DateTime.Now.AddYears(10) - DateTime.Now; // new one is created
                options.XmlRepository = new DataProtectionKeyRepository(dataProtection);
            });
    

    其中DataProtectionKeyRepository是IXmlRepository的实现

    public class DataProtectionKeyRepository : IXmlRepository
    {
     
        private IDataProtectionRepo dataProtectionRepo;
        public DataProtectionKeyRepository(IDataProtectionRepo dataProtectionRepo)
        {
            this.dataProtectionRepo = dataProtectionRepo;
        }
    
        public IReadOnlyCollection<XElement> GetAllElements()
        {
            return new ReadOnlyCollection<XElement>(dataProtectionRepo.GetAll().Select(k => XElement.Parse(k.XmlData)).ToList());
        }
    
        public void StoreElement(XElement element, string friendlyName)
        {
            dataProtectionRepo.AddOrUpdate(new ProtectionKeyModel { Name = friendlyName, XmlData = element.ToString() });
        }
    }
    

    通讯类

    public class ProtectionKeyModel
        {
            public string Name { get; set; }
            public string XmlData { get; set; }
        }
    

    而存储库,可以是数据库、文件系统、云存储,任何适合你的,实现你喜欢的界面

    public interface IDataProtectionRepo
        {
            IEnumerable<ProtectionKeyModel> GetAll();
            void AddOrUpdate(ProtectionKeyModel protectionKeyModel);
        }
    

    【讨论】:

    • 谢谢你的回答,但我的问题不是关于如何存储密钥,我打算将数据保护密钥存储在数据库或文件中,我可以使用默认的密钥存储提供程序供我使用。问题是当我在启动中配置数据保护以将密钥存储在数据库或文件中并运行项目时,我的日志中会出现警告:No XML encryptor configured. Key {b56dffc61e80} may be persisted to storage in unencrypted form.。所以我需要知道我是否需要加密密钥,如果需要,我发现在生产中使用的解决方案是有效的。
    • 我明白了,今天是警告,明天将是错误,所以是的,您应该找到一种方法。
    • 试试这个加密: services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(new DirectoryInfo(@"PATH-HERE")) .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration() { EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC , ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 });
    • 是的,仍然收到同样的警告。
    猜你喜欢
    • 1970-01-01
    • 2017-02-17
    • 2012-11-25
    • 2013-02-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多