共用方式為


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

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

載入設定檔

現在已 ProtectionProfileObserverImpl 定義 ,我們將使用它來具現化 mip::ProtectionProfilemip::ProtectionProfile建立物件需要 mip::ProtectionProfile::Settings

ProtectionProfile::設定 參數

  • std::shared_ptr<MipContext>mip::MipContext:初始化以儲存應用程式資訊、狀態路徑等的物件。
  • mip::CacheStorageType:定義如何儲存狀態:在記憶體、磁片或磁片上,以及加密。
  • std::shared_ptr<mip::ConsentDelegate>:類別 mip::ConsentDelegate 的共用指標。
  • std::shared_ptr<mip::ProtectionProfile::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);

ProtectionProfile::Settings profileSettings(
    mMipContext,                                        // mipContext object
    mip::CacheStorageType::InMemory,                   // use in memory storage    
    std::make_shared<ConsentDelegateImpl>(),           // new consent delegate
    std::make_shared<ProtectionProfileObserverImpl>()); // 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);

ProtectionProfile::Settings profileSettings(
    mMipContext,                                         // mipContext object
    mip::CacheStorageType::OnDisk,                      // use on disk storage    
    std::make_shared<ConsentDelegateImpl>(),            // new consent delegate
    std::make_shared<ProtectionProfileObserverImpl>()); // new protection profile

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

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

如果我們已載入設定檔,且該作業成功, ProtectionProfileObserverImpl::OnLoadSuccess 則會呼叫 我們的 實 mip::ProtectionProfile::Observer::OnLoadSuccess 作。 產生的物件或例外狀況指標以及內容會以參數的形式傳入函式。 內容是我們建立來處理非同步作業的指標 std::promise 。 函式只會將 Promise 的值設定為 ProtectionProfile 物件 (coNtext)。 當 main 函式使用 Future.get() 時,結果可以儲存在新的 物件中。

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

將它放在一起

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

int main()
{
    const string userName = "MyTestUser@contoso.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);

    ProtectionProfile::Settings profileSettings(
        mMipContext,                                    // mipContext object
        mip::CacheStorageType::OnDisk,                 // use on disk storage        
        std::make_shared<ConsentDelegateImpl>(),       // new consent delegate
        std::make_shared<ProfileObserver>());          // new protection profile observer

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

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

後續步驟

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

保護引擎概念