本快速入门介绍如何实现 MIP SDK .NET包装器在运行时使用的客户端初始化模式。
注释
任何使用 MIP .NET 包装器的 Protection SDK 的客户端应用程序都需要执行本快速入门中的步骤。 在完成应用程序初始化以及实现身份验证委托类和同意委托类后,依次完成这些快速入门。
先决条件
如果尚未这样做,请确保:
- 完成 Microsoft信息保护 (MIP) SDK 设置和配置中的步骤。 本“保护配置文件和引擎设置”快速入门依赖于正确的 SDK 安装和配置。
- 可选:
创建 Visual Studio 解决方案和项目
首先,创建并配置初始的 Visual Studio 解决方案和项目,后续其他快速入门都将以此为基础。
打开 Visual Studio 2022 或更高版本,选择 “文件 ”菜单“ 新建”和 “项目”。 在 “新建项目 ”对话框中:
将 MIP 保护 SDK 的 NuGet 包添加到项目:
- 在解决方案资源管理器中,右键单击顶部解决方案节点下的项目节点,然后选择“管理 NuGet 包...”:
- 当 NuGet 包管理器 选项卡在编辑器组的选项卡区域中打开时:
- 选择“浏览”。
- 在搜索框中输入
Microsoft.InformationProtection。 - 选择
Microsoft.InformationProtection.Protection包。 - 选择“安装”,然后在出现“预览更改确认”对话框时选择“确定”。
重复前面的步骤,但这次是将
Microsoft.Identity.Client(MSAL)添加到应用程序中。
实现身份验证委托和同意委托
如果尚未实现,请按照 文件 SDK 应用程序初始化 中列出的步骤实现身份验证和同意委托。
初始化 MIP SDK 托管包装
在 解决方案资源管理器中,打开项目中包含方法实现
Main()的.cs文件。 默认名称与该项目所在的项目名称相同,这是在您创建项目时指定的。删除生成的实现
main()。托管包装器包括一个静态类,
Microsoft.InformationProtection.MIP用于初始化、创建MipContext、加载配置文件和释放资源。 若要初始化保护 SDK 操作的包装器,请调用MIP.Initialize()并传入MipComponent.Protection以加载保护操作所需的库。在
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;
}
}
}
使用以下值替换粘贴的源代码中的占位符值:
Placeholder 价值 Example <application-id> 分配给在“MIP SDK 安装和配置”中注册的应用程序的 Microsoft Entra 应用程序 ID(2 个实例)。 00001111-aaaa-2222-bbbb-3333cccc4444 <友好名称> 应用程序的用户定义的友好名称。 应用初始化 现在执行应用程序的最终构建并解决任何错误。 代码应成功生成。
后续步骤
完成初始化代码后,即可开始体验 MIP 保护 SDK 的下一个快速入门。
- 浏览 GitHub 上的 MIP 保护 SDK .NET 示例。