Ich habe Schwierigkeiten, die Data Protection API zu verstehen.ASP.Net Core Data Protection API in einer Clusterumgebung
Ich möchte einige Net-Core-Web-Anwendungen in einer Cluster-Umgebung (Service Fabric) einrichten. Zuvor sollten Sie lediglich sicherstellen, dass jeder Computer den gleichen Schlüssel in seiner web.config hat. Einfach. Mit der neuen Datenschutz-API scheint es ein bisschen (little!) Etwas beteiligter zu sein.
Aus der Dokumentation here geht hervor, dass es so einfach sein sollte, den Data Protection Service mit dem entsprechenden Zertifikat einzurichten.
Allerdings habe ich versucht, dies:
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();
}
}
Und bekomme ich nur eine Ausnahme von
System.InvalidOperationException occurred
HResult=0x80131509
Message=No service for type 'Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository' has been registered.
die nach einigen umdreht heraus zu graben, dass es ist, weil es keine persistenten Speicher für den Schlüssel angegeben ist.
Was wird hier empfohlen? Sollte ich meine Schlüssel an einer zentralen Stelle beibehalten (d. H. Eine Freigabe, die für alle meine Anwendungen verfügbar ist). Wenn ja, aus welchem Grund?
Sie müssen explizit angeben, wo die Schlüssel beibehalten werden. https://docs.microsoft.com/en-gb/aspnet/core/security/data-protection/configuration/overview - probiere '.PersistKeysToFileSystem (neues DirectoryInfo (@" \\ server \ share \ directory \ ")); zum Beispiel. – Christo
@Christo Danke! Ich dachte, ich hätte diese Frage aktualisiert. Ich habe meine Schlüssel für AzureStorage beibehalten. – Mardoxx