CustomBuilderExtensions.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  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.AspNetCore.DataProtection.KeyManagement;
  6. using Microsoft.AspNetCore.DataProtection.XmlEncryption;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Options;
  9. namespace CustomEncryptorSample
  10. {
  11. public static class CustomBuilderExtensions
  12. {
  13. public static IDataProtectionBuilder UseXmlEncryptor(
  14. this IDataProtectionBuilder builder,
  15. Func<IServiceProvider, IXmlEncryptor> factory)
  16. {
  17. builder.Services.AddSingleton<IConfigureOptions<KeyManagementOptions>>(serviceProvider =>
  18. {
  19. var instance = factory(serviceProvider);
  20. return new ConfigureOptions<KeyManagementOptions>(options =>
  21. {
  22. options.XmlEncryptor = instance;
  23. });
  24. });
  25. return builder;
  26. }
  27. }
  28. }