ProtectionManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.InformationProtection;
  7. using Microsoft.InformationProtection.Protection;
  8. namespace AipGateway.AIP
  9. {
  10. public class ProtectionManager : AbstractManager
  11. {
  12. private IProtectionProfile _profile = null;
  13. private IProtectionEngine _engine = null;
  14. public ProtectionManager()
  15. {
  16. }
  17. ~ProtectionManager() => this.Dispose(false);
  18. public override void Dispose()
  19. {
  20. this.Dispose(true);
  21. GC.SuppressFinalize((object)this);
  22. }
  23. protected virtual void Dispose(bool disposing)
  24. {
  25. lock (this)
  26. {
  27. if (_engine != null) _engine.Dispose();
  28. if (_profile != null) _profile.Dispose();
  29. _profile = null;
  30. _engine = null;
  31. }
  32. }
  33. public override bool CreateProfile(ref MipContext mipContext)
  34. {
  35. try
  36. {
  37. var profileSettings = new ProtectionProfileSettings(mipContext,
  38. CacheStorageType.OnDisk,
  39. new ConsentDelegateImplementation());
  40. // IProtectionProfile은 특정 애플리케이션에 대한 모든 SDK 작업의 루트입니다.
  41. _profile = MIP.LoadProtectionProfile(profileSettings);
  42. }
  43. catch (Exception e)
  44. {
  45. SetError(1, "ProtectionManager::CreateProfile Failed.", e.Message);
  46. return false;
  47. }
  48. return true;
  49. }
  50. public override bool CreateEngine(ref Identity identity, ref AuthDelegateImplementation authDelegate)
  51. {
  52. try
  53. {
  54. authDelegate.ResetError();
  55. var engineSettings = new ProtectionEngineSettings(identity.Email, authDelegate, "", "")
  56. {
  57. Identity = identity
  58. };
  59. _engine = Task.Run(async () => await _profile.AddEngineAsync(engineSettings)).Result;
  60. Console.WriteLine("Protection Engine Templates ======================================================");
  61. var templates = _engine.GetTemplates();
  62. for (int ii = 0; ii < templates.Count; ii++)
  63. {
  64. Console.WriteLine("{0}: {1}, {2}", ii.ToString(), templates[ii].Id + " : " + templates[ii].Name, templates[ii].Description);
  65. }
  66. Console.WriteLine("=======================================================================");
  67. }
  68. catch (Exception e)
  69. {
  70. if (authDelegate.LastErrNo != 0)
  71. {
  72. SetError(authDelegate.LastErrNo, "ProtectionManager::CreateEngine Failed.", authDelegate.LastErrMsg);
  73. }
  74. else
  75. {
  76. SetError(2, "ProtectionManager::CreateEngine Failed.", e.Message);
  77. }
  78. return false;
  79. }
  80. return true;
  81. }
  82. public List<TemplateDescriptor> GetTemplates()
  83. {
  84. return _engine.GetTemplates();
  85. }
  86. }
  87. }