Program.cs 1.5 KB

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