共用方式為


Microsoft 資訊保護 SDK - 原則 SDK 設定檔概念

必須先載入 , mip::Profile 才能執行任何原則 SDK 作業。

下列兩個範例示範如何使用本機儲存體來建立設定檔設定物件,以用於狀態儲存體,以及僅限記憶體內部儲存體。

載入設定檔

現在已 MipContext 定義 和 ProfileObserver ,我們將使用它們來具現化 mip::PolicyProfilemip::PolicyProfile建立 物件需要 mip::PolicyProfile::Settingsmip::MipContext

Profile::設定 參數

PolicyProfile::Settings 構函式接受下列四個參數:

  • const std::shared_ptr<MipContext>mip::MipContext:初始化以儲存應用程式資訊、狀態路徑等的物件。
  • mip::CacheStorageType:定義如何儲存狀態:在記憶體、磁片或磁片上,以及加密。 如需詳細資訊,請參閱快 取儲存體概念
  • std::shared_ptr<mip::PolicyProfile::Observer> observer:設定檔 Observer 實作的共用指標(在 PolicyProfileProtectionProfileFileProfile 中)。

下列兩個範例示範如何使用本機儲存體來建立設定檔設定物件,以用於狀態儲存體,以及僅限記憶體內部儲存體。

僅將狀態儲存在記憶體中

mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };

std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
				                                                                                  "mip_data",
                                                                                        		  mip::LogLevel::Trace,
                                                                                                  false);

std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

PolicyProfile::Settings profileSettings(
    mMipContext,                                  // mipContext object
    mip::CacheStorageType::InMemory,              // use in memory storage
    std::make_shared<PolicyProfileObserverImpl>()); // new protection profile observer

從磁片上的儲存體路徑讀取/寫入設定檔設定

mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };

std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
			                                                                                      "mip_data",
                                                                                       			  mip::LogLevel::Trace,
                                                                                                  false);

std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

PolicyProfile::Settings profileSettings(
    mipContext,                                    // mipContext object
    mip::CacheStorageType::OnDisk,                 // use on disk storage
    std::make_shared<PolicyProfileObserverImpl>());  // new protection profile observer

接下來,使用 promise/future 模式來載入 Profile

auto profilePromise = std::make_shared<std::promise<std::shared_ptr<Profile>>>();
auto profileFuture = profilePromise->get_future();
Profile::LoadAsync(profileSettings, profilePromise);

如果已成功載入設定檔, ProfileObserver::OnLoadSuccess 則會通知我們的 實作 mip::Profile::Observer::OnLoadSuccess 。 在此情況下 mip::Profile ,產生的 物件會以參數的形式傳入觀察者函式,以及內容。

內容 是我們建立來處理非同步作業的指標 std::promise 。 函式只會將 promise 的值設定為針對第一個參數傳入的 Profile 物件。 當 main 函式使用 Future.get() 時,結果可以儲存在呼叫執行緒的新物件中。

//get the future value and store in profile.
auto profile = profileFuture.get();

將它放在一起

完全實作觀察者和驗證委派之後,現在可以完全載入設定檔。 下列程式碼狙擊假設已包含所有必要的標頭。

int main()
{
    const string userName = "MyTestUser@consoto.com";
    const string password = "P@ssw0rd!";
    const string clientId = "MyClientId";

    mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
 
    std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
				                                                                                       "mip_data",
                                                                                        			   mip::LogLevel::Trace,
                                                                                                       false);

    std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

    PolicyProfile::Settings profileSettings(
        mMipContext,                                    // mipContext object
        mip::CacheStorageType::OnDisk,                 // use on disk storage
        std::make_shared<PolicyProfileObserverImpl>());  // new protection profile observer

    auto profilePromise = std::make_shared<promise<shared_ptr<PolicyProfile>>>();
    auto profileFuture = profilePromise->get_future();
    Profile::LoadAsync(profileSettings, profilePromise);
    auto profile = profileFuture.get();
}

最終結果是我們已成功載入設定檔,並儲存在名為 profile 的 物件中。

後續步驟

現在已新增設定檔,下一個步驟是將引擎新增至設定檔。

原則引擎概念