Program.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using Microsoft.AspNetCore.DataProtection;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.WindowsAzure.Storage;
  6. using Microsoft.AspNetCore.DataProtection.Azure.Blob;
  7. namespace AzureBlob
  8. {
  9. public class Program
  10. {
  11. public static void Main(string[] args)
  12. {
  13. var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
  14. var client = storageAccount.CreateCloudBlobClient();
  15. var container = client.GetContainerReference("key-container");
  16. // The container must exist before calling the DataProtection APIs.
  17. // The specific file within the container does not have to exist,
  18. // as it will be created on-demand.
  19. container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
  20. // Configure
  21. var serviceCollection = new ServiceCollection();
  22. serviceCollection.AddLogging();
  23. serviceCollection.AddDataProtection()
  24. .PersistKeysToAzureBlobStorage(container, "keys.xml");
  25. var services = serviceCollection.BuildServiceProvider();
  26. var loggerFactory = services.GetService<ILoggerFactory>();
  27. loggerFactory.AddConsole(Microsoft.Extensions.Logging.LogLevel.Trace);
  28. // Run a sample payload
  29. var protector = services.GetDataProtector("sample-purpose");
  30. var protectedData = protector.Protect("Hello world!");
  31. Console.WriteLine(protectedData);
  32. }
  33. }
  34. }