我做了一些测试来备份我对复制密钥的评论。首先,我创建了下面的代码简单的控制台应用程序:
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection()
.SetApplicationName("my-app")
.PersistKeysToFileSystem(new DirectoryInfo(@"G:\tmp\so\keys"));
var services = serviceCollection.BuildServiceProvider();
var provider = services.GetService<IDataProtectionProvider>();
var protector = provider.CreateProtector("some_purpose");
Console.WriteLine(Convert.ToBase64String(protector.Protect(Encoding.UTF8.GetBytes("hello world"))));
所以,只要创建DI容器,与特定的文件夹中有登记的数据保护密钥,解决和保护的东西。
这个生成的目标文件夹下面的密钥文件:
<?xml version="1.0" encoding="utf-8"?>
<key id="e6cbce11-9afd-43e6-94be-3f6057cb8a87" version="1">
<creationDate>2017-04-10T15:28:18.0565235Z</creationDate>
<activationDate>2017-04-10T15:28:18.0144946Z</activationDate>
<expirationDate>2017-07-09T15:28:18.0144946Z</expirationDate>
<descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<descriptor>
<encryption algorithm="AES_256_CBC" />
<validation algorithm="HMACSHA256" />
<masterKey p4:requiresEncryption="true" xmlns:p4="http://schemas.asp.net/2015/03/dataProtection">
<!-- Warning: the key below is in an unencrypted form. -->
<value>rVDib1M1BjbCqGctcP+N25zb+Xli9VWX46Y7+9tsoGywGnIg4p9K5QTM+c388i0mC0JBSLaFS2pZBRdR49hsLQ==</value>
</masterKey>
</descriptor>
</descriptor>
</key>
正如你看到的,文件是比较简单的。它陈述了创建,激活,到期日期,使用的算法,对反序列化器类的引用,当然还有关键本身。
现在我配置asp.net应用程序(这样,其他应用程序,而不是一个控制台)是这样的:
services.AddDataProtection()
.SetApplicationName("my-app")
.PersistKeysToFileSystem(new DirectoryInfo(@"G:\tmp\so\keys-asp"))
.DisableAutomaticKeyGeneration();
如果您现在尝试运行的应用程序,做一些需要保护 - 它会失败,因为没有密钥和自动密钥生成被禁用。但是,如果我将由控制台应用生成的密钥复制到目标文件夹 - 会愉快地使用它们。
所以要注意平时的安全问题与复制键,这些键(配置SetDefaultKeyLifetime
带)和使用Microsoft.AspNetCore.DataProtection
同一版本的所有应用程序,你共享密钥(因为它的版本是在规定的截止时间关键xml文件) - 你应该没问题。最好在一个地方和所有其他地方设置共享密钥DisableAutomaticKeyGeneration
。
来源
2017-04-10 15:42:55
Evk
在将机器密钥从一台机器复制到另一台机器之前,现在您可以以同样的方式复制这些密钥?虽然请注意,密钥有到期时间(默认90天),因此您应该增加密钥或创建自动刷新过程。 – Evk
谢谢你的回答!我会尝试使用TeamCity文物自动化。令我惊讶的是,官方文档提到使用samba共享来交换这些密钥,因为这增加了单点故障并且通常比使用静态机器键更麻烦。我会尝试将密钥复制到网络服务器。 – WillemdeKok