快速入门:列出保护模板 (C++)
本快速入门介绍如何使用 MIP 保护 SDK 列出可供用户使用的保护模板。
先决条件
如果尚未完成,请确保先完成以下先决条件,然后再继续:
- 首先完成快速入门 - 客户端应用程序初始化 - 保护 SDK (C++),构建一个入门级 Visual Studio 解决方案。 此“列出保护模板”快速入门依赖于上一个快速入门,用于正确创建入门解决方案。
- 可选:查看 RMS 模板概念。
添加逻辑以列出保护模板
使用保护引擎对象添加逻辑以列出可供用户使用的保护模板。
打开你在前文“快速入门 - 客户端应用程序初始化 - 保护 SDK (C++)”中创建的 Visual Studio 解决方案。
使用“解决方案资源管理器”,打开项目中包含
main()
方法实现的 .cpp 文件。 该文件默认与包含它的项目同名,该名称在项目创建期间指定。在文件顶部附近的
using mip::ProtectionEngine;
之后添加以下using
指令:using std::endl;
在
main()
主体的末尾,在最后一个catch
块的右大括号}
下方和return 0;
上方(你在上一个快速入门中离开的位置),插入以下代码:// List protection templates const shared_ptr<ProtectionEngineObserver> engineObserver = std::make_shared<ProtectionEngineObserver>(); // Create a context to pass to 'ProtectionEngine::GetTemplateListAsync'. That context will be forwarded to the // corresponding ProtectionEngine::Observer methods. In this case, we use promises/futures as a simple way to detect // the async operation completes synchronously. auto loadPromise = std::make_shared<std::promise<vector<shared_ptr<mip::TemplateDescriptor>>>>(); std::future<vector<shared_ptr<mip::TemplateDescriptor>>> loadFuture = loadPromise->get_future(); engine->GetTemplatesAsync(engineObserver, loadPromise); auto templates = loadFuture.get(); cout << "*** Template List: " << endl; for (const auto& protectionTemplate : templates) { cout << "Name: " << protectionTemplate->GetName() << " : " << protectionTemplate->GetId() << endl; }
创建 PowerShell 脚本以生成访问令牌
使用以下 PowerShell 脚本生成访问令牌,这是 SDK 在 AuthDelegateImpl::AcquireOAuth2Token
实现中请求的。 该脚本使用你之前在“MIP SDK 安装和配置”中安装的 ADAL.PS 模块中的 Get-ADALToken
cmdlet。
创建一个 PowerShell 脚本文件(扩展名为 .ps1),并将以下脚本复制/粘贴到该文件中:
$authority
和$resourceUrl
稍后会在下一节中更新。- 更新
$appId
和$redirectUri
,以匹配你在 Microsoft Entra 应用注册中指定的值。
$authority = '<authority-url>' # Specified when SDK calls AcquireOAuth2Token() $resourceUrl = '<resource-url>' # Specified when SDK calls AcquireOAuth2Token() $appId = '<app-ID>' # App ID of the Azure AD app registration $redirectUri = '<redirect-uri>' # Redirect URI of the Azure AD app registration $response = Get-ADALToken -Resource $resourceUrl -ClientId $appId -RedirectUri $redirectUri -Authority $authority -PromptBehavior:RefreshSession $response.AccessToken | clip # Copy the access token text to the clipboard
保存脚本文件,以便稍后在客户端应用程序请求时运行它。
生成并测试应用
最后,生成和测试客户端应用程序。
使用 Ctrl+Shift+b(生成解决方案)生成客户端应用程序。 如果没有生成错误,请使用 F5(启动调试)运行应用程序。
如果项目成功生成并运行,则应用程序在 SDK 每次调用
AcquireOAuth2Token()
方法时都会提示输入访问令牌。 如果多次提示并且请求的值相同,则可以重复使用以前生成的令牌:若要为提示生成访问令牌,请返回到 PowerShell 脚本并:
更新
$authority
和$resourceUrl
变量。 它们必须与步骤 #2 中控制台输出中指定的值相匹配。运行 PowerShell 脚本。
Get-ADALToken
cmdlet 触发 Microsoft Entra 身份验证提示,类似于以下示例。 指定步骤 #2 中控制台输出中提供的相同帐户。 成功登录后,访问令牌将被放置在剪贴板上。可能还需要同意,以允许应用程序在登录帐户下运行时访问 MIP API。 如果未预先同意 Microsoft Entra 应用程序注册(如“MIP SDK 安装和配置”中所述),或者使用来自不同租户的帐户(注册应用程序的帐户除外),则会发生这种情况。 只需单击“接受”即可记录同意。
将访问令牌粘贴到步骤 #2 中的提示后,控制台输出应显示保护模板,类似于以下示例:
*** Template List: Name: Confidential \ All Employees : a74f5027-f3e3-4c55-abcd-74c2ee41b607 Name: Highly Confidential \ All Employees : bb7ed207-046a-4caf-9826-647cff56b990 Name: Confidential : 174bc02a-6e22-4cf2-9309-cb3d47142b05 Name: Contoso Employees Only : 667466bf-a01b-4b0a-8bbf-a79a3d96f720 C:\MIP Sample Apps\ProtectionQS\Debug\ProtectionQS.exe (process 8252) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to continue . . .
注意
复制并保存一个或多个保护模板的 ID(例如
f42a3342-8706-4288-bd31-ebb85995028z
),因为你将在下一个快速入门中使用它。
疑难解答
C++ 应用程序执行期间的问题
总结 | 错误消息 | 解决方案 |
---|---|---|
访问令牌不正确 | 发生异常... 访问令牌是否不正确/已过期? API 调用失败:profile_add_engine_async 失败并出现:[class mip::PolicySyncException] 获取策略失败,请求失败,http 状态代码为:401,x-ms-diagnostics:[2000001;reason="无法解析随请求提交的 OAuth 令牌。";error_category="invalid_token"],correlationId:[35bc0023-3727-4eff-8062-000006d5d672]' C:\VSProjects\MipDev\Quickstarts\AppInitialization\x64\Debug\AppInitialization.exe(进程 29924)已退出,代码为 0。 按任意键关闭此窗口。 。 . |
如果项目成功生成,但你看到与左侧类似的输出,则 AcquireOAuth2Token() 方法中可能有一个无效或过期的令牌。 返回到创建 PowerShell 脚本以生成访问令牌并重新生成访问令牌,再次更新 AcquireOAuth2Token() ,然后重新生成/重新测试。 还可以使用 jwt.ms 单页 Web 应用检查和验证令牌及其声明。 |
后续步骤
现在你已经了解了如何列出可供经过身份验证的用户使用的保护模板,请尝试下一个快速入门:
[加密和解密文本](quick-protection-encrypt-decrypt text-cpp.md)