Quickstart: Initialisatie van clienttoepassingen - Beleids-SDK (C++)

In deze quickstart ziet u hoe u tijdens runtime het clientinitialisatiepatroon implementeert dat wordt gebruikt door de MIP C++ SDK Policy SDK.

Opmerking

De stappen die in deze quickstart worden beschreven, zijn vereist voor elke clienttoepassing die gebruikmaakt van de MIP Policy SDK. Deze quickstart moet worden voltooid na het instellen en configureren van de MIP SDK.

Vereiste voorwaarden

Als u dat nog niet hebt gedaan, moet u het volgende doen:

Een Visual Studio-oplossing en -project maken

  1. Open Visual Studio 2022 of hoger en selecteer het menu Bestand , Nieuw, Project.

    • Selecteer Console-app onder C++-sjablonen.
    • Geef een naam en locatie op voor het project.
  2. Voeg het NuGet-pakket voor de MIP Policy SDK toe aan uw project:

    • Klik in Solution Explorer met de rechtermuisknop op het projectknooppunt en selecteer NuGet-pakketten beheren....
    • Selecteer Bladeren en voer Microsoft.InformationProtection in het zoekvak in.
    • Selecteer het pakket Microsoft.InformationProtection.Policy en klik op Installeren.

Een waarnemersklasse implementeren

Maak een eenvoudige implementatie voor een waarnemersklasse voor een beleidsprofiel door de klasse van mip::PolicyProfile::Observer de SDK uit te breiden.

  1. Voeg een nieuwe klasse toe aan uw project met de naam profile_observer.

  2. Vervang de inhoud van 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. Vervang de inhoud van 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);
    }
    

Een verificatiedelegatie en hoofdfunctie implementeren

  1. Voeg een nieuwe klasse toe aan uw project met de naam auth_delegate. Zie verificatieconcepten voor meer informatie over het implementeren van de mip::AuthDelegate interface.

  2. Werk main() bij om het MipContext te maken, een PolicyProfile te laden en een 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;
    }
    
  3. Bouwen en testen. De toepassing moet met succes worden geïnitialiseerd en verbinding maken met de Policy-service.

Volgende stappen