快速入门:保护 SDK 的客户端应用程序初始化 (C#)

本快速入门介绍如何实现 MIP SDK .NET包装器在运行时使用的客户端初始化模式。

注释

任何使用 MIP .NET 包装器的 Protection SDK 的客户端应用程序都需要执行本快速入门中的步骤。 在完成应用程序初始化以及实现身份验证委托类和同意委托类后,依次完成这些快速入门。

先决条件

如果尚未这样做,请确保:

创建 Visual Studio 解决方案和项目

首先,创建并配置初始的 Visual Studio 解决方案和项目,后续其他快速入门都将以此为基础。

  1. 打开 Visual Studio 2022 或更高版本,选择 “文件 ”菜单“ 新建”和 “项目”。 在 “新建项目 ”对话框中:

    • 在左窗格中的 “已安装”下,选择“ Visual C#”,选择 “Windows 桌面”。

    • 在中心窗格中,选择“控制台应用”(.NET框架)。

    • 在底部窗格中,相应地更新项目 名称、 位置和包含 的解决方案名称 。

    • 完成后,选择右下角的 “确定 ”。

      创建 Visual Studio 解决方案。

  2. 将 MIP 保护 SDK 的 NuGet 包添加到项目:

    • 在解决方案资源管理器中,右键单击顶部解决方案节点下的项目节点,然后选择“管理 NuGet 包...”:
    • 当 NuGet 包管理器 选项卡在编辑器组的选项卡区域中打开时:
      • 选择“浏览”。
      • 在搜索框中输入 Microsoft.InformationProtection。
      • 选择 Microsoft.InformationProtection.Protection 包。
      • 选择“安装”,然后在出现“预览更改确认”对话框时选择“确定”。
  3. 重复前面的步骤,但这次是将 Microsoft.Identity.Client(MSAL)添加到应用程序中。

如果尚未实现,请按照 文件 SDK 应用程序初始化 中列出的步骤实现身份验证和同意委托。

初始化 MIP SDK 托管包装

  1. 在 解决方案资源管理器中,打开项目中包含方法实现 Main() 的.cs文件。 默认名称与该项目所在的项目名称相同,这是在您创建项目时指定的。

  2. 删除生成的实现main()。

  3. 托管包装器包括一个静态类, Microsoft.InformationProtection.MIP用于初始化、创建 MipContext、加载配置文件和释放资源。 若要初始化保护 SDK 操作的包装器,请调用 MIP.Initialize() 并传入 MipComponent.Protection 以加载保护操作所需的库。

  4. 在Main()Program.cs中添加以下代码,将<应用程序 ID> 替换为之前创建Microsoft Entra应用程序注册的 ID。

using System;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Exceptions;
using Microsoft.InformationProtection.Protection;

namespace mip_sdk_dotnet_quickstart
{
    class Program
    {
        private const string clientId = "<application-id>";
        private const string appName = "<friendly-name>";

        static void Main(string[] args)
        {
            //Initialize Wrapper for Protection SDK operations
            MIP.Initialize(MipComponent.Protection);
        }
    }
}

构造保护配置文件和引擎

如上所述,使用 MIP API 的 SDK 客户端需要配置文件和引擎对象。 通过添加代码来加载本地 DLL,然后实例化配置对象和引擎对象,完成本快速入门中的编码部分。

using System;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Exceptions;
using Microsoft.InformationProtection.Protection;

namespace mip_sdk_dotnet_quickstart
{
     class Program
     {
          private const string clientId = "<application-id>";
          private const string appName = "<friendly-name>";

          static void Main(string[] args)
          {
               // Initialize Wrapper for Protection SDK operations.
               MIP.Initialize(MipComponent.Protection);

               // Create ApplicationInfo, setting the clientID from Azure AD App Registration as the ApplicationId.
               ApplicationInfo appInfo = new ApplicationInfo()
               {
                    ApplicationId = clientId,
                    ApplicationName = appName,
                    ApplicationVersion = "1.0.0"
               };

               // Instantiate the AuthDelegateImpl object, passing in AppInfo.
               AuthDelegateImplementation authDelegate = new AuthDelegateImplementation(appInfo);

               // Create MipConfiguration Object
               MipConfiguration mipConfiguration = new MipConfiguration(appInfo, "mip_data", LogLevel.Trace, false, CacheStorageType.OnDiskEncrypted);

               // Create MipContext using Configuration
               MipContext mipContext = MIP.CreateMipContext(mipConfiguration);
                
               // Initialize and instantiate the ProtectionProfile.
               // Create the ProtectionProfileSettings object.
               // Initialize protection profile settings to create/use local state.
               var profileSettings = new ProtectionProfileSettings(mipContext,
                                        CacheStorageType.OnDiskEncrypted,                                        
                                        new ConsentDelegateImplementation());

               // Load the Profile async and wait for the result.
               var protectionProfile = Task.Run(async () => await MIP.LoadProtectionProfileAsync(profileSettings)).Result;

               // Create a ProtectionEngineSettings object, then use that to add an engine to the profile.
               var engineSettings = new ProtectionEngineSettings("user1@tenant.com", authDelegate, "", "en-US");
               engineSettings.Identity = new Identity("user1@tenant.com");
               var protectionEngine = Task.Run(async () => await protectionProfile.AddEngineAsync(engineSettings)).Result;

               // Application Shutdown
               // handler = null; // This will be used in later quick starts.
               protectionEngine = null;
               protectionProfile = null;
               mipContext = null;
          }
     }
}
  1. 使用以下值替换粘贴的源代码中的占位符值:

    Placeholder 价值 Example
    <application-id> 分配给在“MIP SDK 安装和配置”中注册的应用程序的 Microsoft Entra 应用程序 ID(2 个实例)。 00001111-aaaa-2222-bbbb-3333cccc4444
    <友好名称> 应用程序的用户定义的友好名称。 应用初始化
  2. 现在执行应用程序的最终构建并解决任何错误。 代码应成功生成。

后续步骤

完成初始化代码后,即可开始体验 MIP 保护 SDK 的下一个快速入门。