本快速入门介绍如何在运行时实现 MIP C++ SDK 策略 SDK 使用的客户端初始化模式。
注释
使用 MIP 策略 SDK 的任何客户端应用程序都需要本快速入门中的步骤。 在开始之前完成 设置和配置 MIP SDK 。
先决条件
如果尚未这样做,请确保:
- Microsoft 信息保护 (MIP) SDK 设置和配置。 在开始本快速入门之前,请先完成以下步骤。
- 可选:
创建 Visual Studio 解决方案和项目
打开 Visual Studio 2022 或更高版本,选择 “文件 ”菜单“ 新建”和 “项目”。
- 在 C++ 模板下选择 控制台应用 。
- 提供项目 的名称 和 位置 。
将 MIP 策略 SDK 的 NuGet 包添加到项目:
- 在解决方案资源管理器中,右键单击项目节点并选择“管理 NuGet 包...”。
- 选择 “浏览 ”并在搜索框中输入“Microsoft.InformationProtection”。
- 选择Microsoft。InformationProtection.Policy 包并选择“安装”。
实现一个观察程序类
通过扩展 SDK 的 mip::PolicyProfile::Observer 类,为 Policy 配置文件观察程序类创建基本实现。
向您的项目添加一个名为
profile_observer的新类。替换
profile_observer.h的内容:#include <memory> #include "mip/upe/policy_profile.h" class PolicyProfileObserver final : public mip::PolicyProfile::Observer { public: PolicyProfileObserver() { } void OnLoadSuccess(const std::shared_ptr<mip::PolicyProfile>& profile, const std::shared_ptr<void>& context) override; void OnLoadFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override; void OnAddEngineSuccess(const std::shared_ptr<mip::PolicyEngine>& engine, const std::shared_ptr<void>& context) override; void OnAddEngineFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override; };替换
profile_observer.cpp的内容:#include "profile_observer.h" #include <future> using std::promise; using std::shared_ptr; using std::exception_ptr; void PolicyProfileObserver::OnLoadSuccess(const shared_ptr<mip::PolicyProfile>& profile, const shared_ptr<void>& context) { auto loadPromise = static_cast<promise<shared_ptr<mip::PolicyProfile>>*>(context.get()); loadPromise->set_value(profile); } void PolicyProfileObserver::OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) { auto loadPromise = static_cast<promise<shared_ptr<mip::PolicyProfile>>*>(context.get()); loadPromise->set_exception(error); } void PolicyProfileObserver::OnAddEngineSuccess(const shared_ptr<mip::PolicyEngine>& engine, const shared_ptr<void>& context) { auto addEnginePromise = static_cast<promise<shared_ptr<mip::PolicyEngine>>*>(context.get()); addEnginePromise->set_value(engine); } void PolicyProfileObserver::OnAddEngineFailure(const exception_ptr& error, const shared_ptr<void>& context) { auto addEnginePromise = static_cast<promise<shared_ptr<mip::PolicyEngine>>*>(context.get()); addEnginePromise->set_exception(error); }
实现身份验证委托和主函数
向您的项目添加一个名为
auth_delegate的新类。 有关实现接口的详细信息,请参阅mip::AuthDelegate。更新
main()以创建MipContext、加载PolicyProfile和添加:PolicyEngine#include "mip/mip_context.h" #include "mip/upe/policy_profile.h" #include "auth_delegate.h" #include "profile_observer.h" #include <iostream> #include <future> using std::cout; using std::endl; using std::make_shared; using std::shared_ptr; int main() { // Construct/initialize objects required by the application's profile object mip::ApplicationInfo appInfo{ "your_app_id", // Application ID from Microsoft Entra app registration "MIP SDK Policy Quickstart", // Friendly name "1.0" // Version }; auto mipConfiguration = make_shared<mip::MipConfiguration>( appInfo, // ApplicationInfo object "mip_data", // Path to store settings and logs mip::LogLevel::Trace, // Log level false, // isOfflineOnly mip::CacheStorageType::OnDiskEncrypted // Cache storage type ); auto mipContext = mip::MipContext::Create(mipConfiguration); auto profileObserver = make_shared<PolicyProfileObserver>(); auto authDelegateImpl = make_shared<sample::auth::AuthDelegateImpl>("your_app_id"); mip::PolicyProfile::Settings profileSettings(mipContext, mip::CacheStorageType::OnDiskEncrypted, profileObserver ); // Load Policy Profile auto profilePromise = make_shared<std::promise<shared_ptr<mip::PolicyProfile>>>(); auto profileFuture = profilePromise->get_future(); mip::PolicyProfile::LoadAsync(profileSettings, profilePromise); auto profile = profileFuture.get(); // Add a Policy Engine mip::PolicyEngine::Settings engineSettings( mip::Identity("user@contoso.com"), authDelegateImpl, "", "en-US", false ); auto enginePromise = make_shared<std::promise<shared_ptr<mip::PolicyEngine>>>(); auto engineFuture = enginePromise->get_future(); profile->AddEngineAsync(engineSettings, enginePromise); auto engine = engineFuture.get(); // Application using Policy engine is ready. // Engine can be used to list labels, compute actions, etc. cout << "Policy engine loaded successfully." << endl; return 0; }构建和测试。 应用程序应成功初始化并连接到策略服务。