빠른 시작: 클라이언트 애플리케이션 초기화 - 정책 SDK(C++)

이 빠른 시작에서는 런타임에 MIP C++ SDK 정책 SDK에서 사용하는 클라이언트 초기화 패턴을 구현하는 방법을 보여 줍니다.

메모

MIP 정책 SDK를 사용하는 모든 클라이언트 애플리케이션에는 이 빠른 시작의 단계가 필요합니다. 시작하기 전에 MIP SDK 설정을 완료하고 구성 합니다.

사전 요구 사항

이미 하지 않았다면, 반드시 다음을 수행하세요:

Visual Studio 솔루션 및 프로젝트 만들기

  1. Visual Studio 2022 이상을 열고 파일 메뉴, 새로 만들기, 프로젝트를 선택합니다.

    • C++ 템플릿에서 콘솔 앱을 선택합니다.
    • 프로젝트의 이름과위치를 제공합니다.
  2. MIP 정책 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);
    }
    

인증 대리자 및 main 함수 구현

  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. 빌드 및 테스트. 애플리케이션이 성공적으로 초기화되고 정책 서비스에 연결되어야 합니다.

다음 단계