Program.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using Microsoft.AspNetCore.DataProtection;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Logging;
  5. using StackExchange.Redis;
  6. namespace Redis
  7. {
  8. public class Program
  9. {
  10. public static void Main(string[] args)
  11. {
  12. // Connect
  13. var redis = ConnectionMultiplexer.Connect("localhost:6379");
  14. // Configure
  15. var serviceCollection = new ServiceCollection();
  16. serviceCollection.AddLogging();
  17. serviceCollection.AddDataProtection()
  18. .PersistKeysToRedis(redis, "DataProtection-Keys");
  19. var services = serviceCollection.BuildServiceProvider();
  20. var loggerFactory = services.GetService<ILoggerFactory>();
  21. loggerFactory.AddConsole(LogLevel.Trace);
  22. // Run a sample payload
  23. var protector = services.GetDataProtector("sample-purpose");
  24. var protectedData = protector.Protect("Hello world!");
  25. Console.WriteLine(protectedData);
  26. }
  27. }
  28. }