1
我很难理解Data Protection API。集群环境中的ASP.Net核心数据保护API
我想在集群环境(服务结构)中设置一些网络核心Web应用程序。以前你要做的只是确保每台机器在其web.config中都有相同的密钥。简单。有了新的数据保护API,它似乎有点牵涉其中(lottle!)。
从文档here看来,它应该与使用适当的证书设置数据保护服务一样简单。
不过我想这一点:
public static void Main(string[] args)
{
// add data protection services
var serviceCollection = new ServiceCollection();
string thumbPrint = "XXXXXXXXXXXX";
serviceCollection.AddDataProtection()
.ProtectKeysWithDpapiNG($"CERTIFICATE=HashId:{thumbPrint}", flags: Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiNGProtectionDescriptorFlags.None);
var services = serviceCollection.BuildServiceProvider();
// create an instance of MyClass using the service provider
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
instance.RunSample();
}
public class MyClass
{
IDataProtector _protector;
// the 'provider' parameter is provided by DI
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.Write("Enter input: ");
string input = Console.ReadLine();
// protect the payload
string protectedPayload = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayload}");
// unprotect the payload
string unprotectedPayload = _protector.Unprotect(protectedPayload);
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
Console.ReadLine();
}
}
我刚刚得到的
System.InvalidOperationException occurred
HResult=0x80131509
Message=No service for type 'Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository' has been registered.
一个异常,经过一番周围挖原来,这是因为没有为键没有指定持久化存储。
这里有什么建议?我是否应该将密钥持久保存到某个中央位置(即可用于所有应用程序的共享位置)。如果是这样,原因是什么?
你需要明确哪些键是持久的。 https://docs.microsoft.com/en-gb/aspnet/core/security/data-protection/configuration/overview - 尝试'.PersistKeysToFileSystem(new DirectoryInfo(@“\\ server \ share \ directory \”)); '例如。 – Christo
@基督谢谢!完全以为我更新了这个问题,我最终坚持把我的密钥保存到AzureStorage。 – Mardoxx