Program.cs 1.6 KB

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