這個快速入門說明如何在執行時實作 MIP C++ SDK 政策 SDK 所使用的用戶端初始化模式。
備註
本快速入門中所述的步驟,是任何使用 MIP 政策 SDK 的用戶端應用程式所必需的。 這個快速入門應該在 設定 MIP SDK 後完成。
先決條件
如果您尚未這麼做,請務必:
建立 Visual Studio 方案和專案
開啟 Visual Studio 2022 或更新版本,選擇 檔案 選單, 新增, 專案。
- 在 C++ 範本中選擇 Console App 。
- 請提供專案 名稱 與 地點 。
將 MIP Policy SDK 的 NuGet 套件加入您的專案:
- 在 解決方案總管中,右鍵點擊專案節點,選擇 管理 NuGet 套件......。
- 選擇 瀏覽 ,並在搜尋框輸入「Microsoft.InformationProtection」。
- 選擇 Microsoft.InformationProtection.Policy 套件並點 選安裝。
實作一個觀察者類別
透過擴充 SDK 的 mip::PolicyProfile::Observer 類別,建立一個專為策略設定檔觀察者類別的基本實作。
在你的專案中新增一個名為
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 auto mipConfiguration = make_shared<mip::MipConfiguration>( "your_app_id", // Application ID from Microsoft Entra app registration "MIP SDK Policy Quickstart", // Friendly name "1.0", // Version true // GUID for each machine ); 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, authDelegateImpl ); // Load Policy Profile auto profilePromise = make_shared<std::promise<shared_ptr<mip::PolicyProfile>>>(); auto profileFuture = profilePromise->get_future(); mip::PolicyProfile::LoadAsync(profileSettings, profileObserver, 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, profileObserver, 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; }組裝並測試。 應用程式應成功初始化並連接至政策服務。