using System; using System.IO; using System.Threading; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.Extensions.DependencyInjection; public class Program { public static void Main(string[] args) { var serviceCollection = new ServiceCollection(); serviceCollection.AddDataProtection() // point at a specific folder and use DPAPI to encrypt keys .PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp-keys")) .ProtectKeysWithDpapi(); var services = serviceCollection.BuildServiceProvider(); // perform a protect operation to force the system to put at least // one key in the key ring services.GetDataProtector("Sample.KeyManager.v1").Protect("payload"); Console.WriteLine("Performed a protect operation."); Thread.Sleep(2000); // get a reference to the key manager var keyManager = services.GetService(); // list all keys in the key ring var allKeys = keyManager.GetAllKeys(); Console.WriteLine($"The key ring contains {allKeys.Count} key(s)."); foreach (var key in allKeys) { Console.WriteLine($"Key {key.KeyId:B}: Created = {key.CreationDate:u}, IsRevoked = {key.IsRevoked}"); } // revoke all keys in the key ring keyManager.RevokeAllKeys(DateTimeOffset.Now, reason: "Revocation reason here."); Console.WriteLine("Revoked all existing keys."); // add a new key to the key ring with immediate activation and a 1-month expiration keyManager.CreateNewKey( activationDate: DateTimeOffset.Now, expirationDate: DateTimeOffset.Now.AddMonths(1)); Console.WriteLine("Added a new key."); // list all keys in the key ring allKeys = keyManager.GetAllKeys(); Console.WriteLine($"The key ring contains {allKeys.Count} key(s)."); foreach (var key in allKeys) { Console.WriteLine($"Key {key.KeyId:B}: Created = {key.CreationDate:u}, IsRevoked = {key.IsRevoked}"); } } } /* * SAMPLE OUTPUT * * Performed a protect operation. * The key ring contains 1 key(s). * Key {1b948618-be1f-440b-b204-64ff5a152552}: Created = 2015-03-18 22:20:49Z, IsRevoked = False * Revoked all existing keys. * Added a new key. * The key ring contains 2 key(s). * Key {1b948618-be1f-440b-b204-64ff5a152552}: Created = 2015-03-18 22:20:49Z, IsRevoked = True * Key {2266fc40-e2fb-48c6-8ce2-5fde6b1493f7}: Created = 2015-03-18 22:20:51Z, IsRevoked = False */