Program.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.IO;
  5. using Microsoft.AspNetCore.DataProtection;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. namespace CustomEncryptorSample
  9. {
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. var keysFolder = Path.Combine(Directory.GetCurrentDirectory(), "temp-keys");
  15. var serviceCollection = new ServiceCollection();
  16. serviceCollection.AddLogging();
  17. serviceCollection.AddDataProtection()
  18. .PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
  19. .UseXmlEncryptor(s => new CustomXmlEncryptor(s));
  20. var services = serviceCollection.BuildServiceProvider();
  21. var loggerFactory = services.GetRequiredService<LoggerFactory>();
  22. loggerFactory.AddConsole();
  23. var protector = services.GetDataProtector("SamplePurpose");
  24. // protect the payload
  25. var protectedPayload = protector.Protect("Hello World!");
  26. Console.WriteLine($"Protect returned: {protectedPayload}");
  27. // unprotect the payload
  28. var unprotectedPayload = protector.Unprotect(protectedPayload);
  29. Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
  30. }
  31. }
  32. }