Program.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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 StackExchange.Redis;
  8. namespace Redis
  9. {
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. // Connect
  15. var redis = ConnectionMultiplexer.Connect("localhost:6379");
  16. // Configure
  17. var serviceCollection = new ServiceCollection();
  18. serviceCollection.AddLogging();
  19. serviceCollection.AddDataProtection()
  20. .PersistKeysToRedis(redis, "DataProtection-Keys");
  21. var services = serviceCollection.BuildServiceProvider();
  22. var loggerFactory = services.GetService<LoggerFactory>();
  23. loggerFactory.AddConsole();
  24. // Run a sample payload
  25. var protector = services.GetDataProtector("sample-purpose");
  26. var protectedData = protector.Protect("Hello world!");
  27. Console.WriteLine(protectedData);
  28. }
  29. }
  30. }