クイック スタート: クライアント アプリケーションの初期化 - Policy SDK (C++)

このクイック スタートでは、実行時に MIP C++ SDK ポリシー SDK によって使用されるクライアント初期化パターンを実装する方法について説明します。

注

MIP Policy SDK を使用するすべてのクライアント アプリケーションには、このクイック スタートの手順が必要です。 開始する前に 、MIP SDK の設定と構成 を完了します。

前提条件

まだ行っていない場合は、次の手順を実行してください。

Visual Studio ソリューションとプロジェクトを作成する

  1. Visual Studio 2022 以降を開き、[ ファイル ] メニューの [ 新規作成]、[ プロジェクト] の順に選択します。

    • C++ テンプレートで [コンソール アプリ ] を選択します。
    • プロジェクトの 名前 と 場所 を指定します。
  2. MIP Policy SDK の NuGet パッケージをプロジェクトに追加します。

    • ソリューション エクスプローラーで、プロジェクト ノードを右クリックし、[NuGet パッケージの管理...] を選択します。
    • [参照] を選択し、検索ボックスに「Microsoft.InformationProtection」と入力します。
    • Microsoft.InformationProtection.Policy パッケージを選択し、[インストール] を選択します。

オブザーバー クラスを実装する

SDK の mip::PolicyProfile::Observer クラスを拡張して、ポリシー プロファイル オブザーバー クラスの基本的な実装を作成します。

  1. profile_observerという名前の新しいクラスをプロジェクトに追加します。

  2. 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;
    };
    
  3. 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);
    }
    

認証デリゲートとメイン関数を実装する

  1. auth_delegateという名前の新しいクラスをプロジェクトに追加します。 インターフェイスの実装の詳細については、mip::AuthDelegateを参照してください。

  2. 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;
    }
    
  3. ビルドとテスト。 アプリケーションは正常に初期化され、ポリシー サービスに接続されます。

次のステップ