共用方式為


Microsoft 資訊保護 SDK - 原則 SDK 引擎概念

mip::PolicyEngine 會實作原則 SDK 可以執行的所有作業,但載入設定檔除外。

實作:新增原則引擎

實作:建立原則引擎設定

與設定檔類似,引擎也需要設定物件 。 mip::PolicyEngine::Settings 此物件會儲存唯一的引擎識別碼、實 mip::AuthDelegate 作的物件、可用於偵錯或遙測的可自訂用戶端資料,以及選擇性地儲存地區設定。

在這裡,我們會使用應用程式使用者的身分識別來建立 FileEngine::Settings 名為 engine 的物件設定

PolicyEngine::Settings engineSettings(
  mip::Identity(mUsername), // mip::Identity.  
  authDelegateImpl,         // Auth delegate object
  "",                       // Client data. Customizable by developer, stored with engine.
  "en-US",                  // Locale.
  false);                   // Load sensitive information types for driving classification.

以這種方式建立引擎設定請務必透過下列方式明確設定唯一的 engineId:

engineSettings.SetEngineId(engineId);

使用使用者名稱或電子郵件 有助於確保每次使用者使用服務或應用程式時,都會載入相同的引擎。

此外,也提供自訂引擎識別碼:

PolicyEngine::Settings engineSettings(
  "myEngineId",     // String
  authDelegateImpl, // Auth delegate object
  "",               // Client data in string format. Customizable by developer, stored with engine.
  "en-US",          // Locale. Default is en-US
  false);           // Load sensitive information types for driving classification. Default is false.

最佳做法是,第一個參數 id 應該是一些可讓引擎輕鬆連線至相關聯使用者的專案,最好是使用者主體名稱。

實作:新增原則引擎

若要新增引擎,我們將回到用來載入設定檔的未來/承諾模式。 我們不會為 mip::Profile 建立承諾,而是使用 mip::PolicyEngine


  // Auto profile will be std::shared_ptr<mip::Profile>.
  auto profile = profileFuture.get();

  // Create the delegate
  auto authDelegateImpl = std::make_shared<sample::auth::AuthDelegateImpl>(appInfo, userName, password);


  // Create the PolicyEngine::Settings object.
  PolicyEngine::Settings engineSettings("UniqueID", authDelegateImpl, "");

  // Create a promise for std::shared_ptr<mip::PolicyEngine>.
  auto enginePromise = std::make_shared<std::promise<std::shared_ptr<mip::PolicyEngine>>>();

  // Instantiate the future from the promise.
  auto engineFuture = enginePromise->get_future();

  // Add the engine using AddEngineAsync, passing in the engine settings and the promise.
  profile->AddEngineAsync(engineSettings, enginePromise);

  // Get the future value and store in std::shared_ptr<mip::PolicyEngine>.
  auto engine = engineFuture.get();

上述程式碼的最終結果是,我們已成功將已驗證使用者的引擎新增至設定檔。

實作:列出敏感度標籤

使用新增的引擎,現在可以藉由呼叫 engine->ListSensitivityLabels() 來列出已驗證使用者可用的所有敏感度標籤。

ListSensitivityLabels() 會從服務擷取特定使用者的標籤和屬性清單。 結果會儲存在 的 std::shared_ptr<mip::Label> 向量中。

實作:ListSensitivityLabels()

std::vector<shared_ptr<mip::Label>> labels = engine->ListSensitivityLabels();

實作:列印標籤

//Iterate through all labels in the vector
for (const auto& label : labels) {
  //print the label name
  cout << label->GetName() << endl;
  //Iterate through all child labels
  for (const auto& child : label->GetChildren()) {
    //Print the label with some formatting
    cout << "->  " << child->GetName() << endl;
  }
}

列印名稱是顯示我們已成功從服務提取原則並能夠取得標籤的簡單方式。 若要套用標籤,則需要標籤識別碼。 修改上述的狙擊,以傳回標籤識別碼的結果如下:

for (const auto& label : labels) {
  //Print label name and GUID
  cout << label->GetName() << " : " << label->GetId() << endl;

  //Print child label name and GUID
  for (const auto& child : label->GetChildren()) {
    cout << "->  " << child->GetName() <<  " : " << child->GetId() << endl;
  }
}

ListSensitivityLabels() 回的 mip::Label 集合可用來顯示使用者可用的所有標籤,然後在選取時,使用識別碼將標籤套用至檔案。