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