Program.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 System.Runtime.InteropServices;
  6. using Microsoft.AspNetCore.DataProtection;
  7. using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
  8. namespace NonDISample
  9. {
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. var keysFolder = Path.Combine(Directory.GetCurrentDirectory(), "temp-keys");
  15. // instantiate the data protection system at this folder
  16. var dataProtectionProvider = DataProtectionProvider.Create(
  17. new DirectoryInfo(keysFolder),
  18. configuration =>
  19. {
  20. configuration.SetApplicationName("my app name");
  21. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  22. {
  23. configuration.ProtectKeysWithDpapi();
  24. }
  25. });
  26. var protector = dataProtectionProvider.CreateProtector("Program.No-DI");
  27. // protect the payload
  28. var protectedPayload = protector.Protect("Hello World!");
  29. Console.WriteLine($"Protect returned: {protectedPayload}");
  30. // unprotect the payload
  31. var unprotectedPayload = protector.Unprotect(protectedPayload);
  32. Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
  33. }
  34. }
  35. }