CustomXmlEncryptor.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 System.Xml.Linq;
  5. using Microsoft.AspNetCore.DataProtection.XmlEncryption;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. namespace CustomEncryptorSample
  9. {
  10. public class CustomXmlEncryptor : IXmlEncryptor
  11. {
  12. private readonly ILogger _logger;
  13. public CustomXmlEncryptor(IServiceProvider services)
  14. {
  15. _logger = services.GetRequiredService<ILoggerFactory>().CreateLogger<CustomXmlEncryptor>();
  16. }
  17. public EncryptedXmlInfo Encrypt(XElement plaintextElement)
  18. {
  19. if (plaintextElement == null)
  20. {
  21. throw new ArgumentNullException(nameof(plaintextElement));
  22. }
  23. _logger.LogInformation("Not encrypting key");
  24. var newElement = new XElement("unencryptedKey",
  25. new XComment(" This key is not encrypted. "),
  26. new XElement(plaintextElement));
  27. var encryptedTextElement = new EncryptedXmlInfo(newElement, typeof(CustomXmlDecryptor));
  28. return encryptedTextElement;
  29. }
  30. }
  31. }