Microsoft 資訊保護 檔案 SDK - 降低檔案上敏感度標籤的動作理由 (C++)

本快速入門說明當標籤原則需要理由時,處理降級標籤作業。 在這裡,我們將使用 mip::FileHandler 類別來變更檔案的標籤。 如需進一步的詳細資料,請參閱 API 參考

必要條件

如果您尚未完成,請務必先完成下列必要條件,再繼續進行:

新增邏輯以將較低標籤設定為受保護的檔案

使用 mip::FileHandler 物件,新增邏輯以在檔案上設定敏感度標籤。

  1. 開啟您在上 一個快速入門中建立的 Visual Studio 解決方案:設定/取得敏感度標籤(C++)。

  2. 使用 方案總管,在您的專案中開啟包含 方法實作的 main() .cpp 檔案。 它預設為與您在專案建立期間指定的專案相同名稱。

  3. 在檔案頂端的對應現有指示詞下方,新增下列 #include 和 using 指示詞:

    
        #include "mip/file/file_error.h"
    
        using mip::JustificationRequiredError;
        using std::cin;
    
    
  4. <label-id> 上一個快速入門中的值更新為敏感度標籤,這需要降低的理由。 在本快速入門執行期間,我們會先設定此標籤,然後嘗試在進一步的步驟中透過程式碼片段將其降低。

  5. 在本文結尾 main() 處,下方 system("pause"); 和上方的關機區塊(您在上一個快速入門中離開的位置),插入下列程式碼:


// Downgrade label
// Set paths and lower label ID
// Set a new label on input file.

string lowerlabelId = "<lower-label-id>";
cout << "\nApplying new Label ID " << lowerlabelId << " to " << filePathOut << endl;
mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);

// Try to apply a label with lower sensitivity.
try
{
    handler->SetLabel(engine->GetLabelById(lowerlabelId), labelingOptions, mip::ProtectionSettings());
}

catch (const mip::JustificationRequiredError& e)
{
    // Request justification from user.
    cout<<"Please provide justification for downgrading a label: "<<endl;
    string justification;
    cin >> justification;

    // Set Justification provided flag
    bool isDowngradeJustified = true;
    mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
    labelingOptions.SetDowngradeJustification(isDowngradeJustified,justification);

    //Set new label.
    handler->SetLabel(engine->GetLabelById(lowerlabelId), labelingOptions, mip::ProtectionSettings());
}

catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid label ID?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}

// Commit changes, save as a different output file
string lowerFilePathOut = "<lower-output-file-path>";
try
{
    cout << "Committing changes" << endl;
    auto commitPromise = std::make_shared<std::promise<bool>>();
    auto commitFuture = commitPromise->get_future();
    handler->CommitAsync(lowerFilePathOut, commitPromise);
    if (commitFuture.get()) {
        cout << "\nLabel committed to file: " << lowerFilePathOut << endl;
    }
    else {
        cout << "Failed to label: " + lowerFilePathOut << endl;
        return 1;
    }
}
catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid commit file path?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}
system("pause");

// Set up async FileHandler for output file operations
string lowerActualFilePath = "<lower-content-identifier>";
try
{
    auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
    auto handlerFuture = handlerPromise->get_future();
    engine->CreateFileHandlerAsync(
        lowerFilePathOut,
        lowerActualFilePath,
        true,
        std::make_shared<FileHandlerObserver>(),
        handlerPromise);

    handler = handlerFuture.get();
}
catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid output file path?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}

// Get the lowered label from output file
try
{
    cout << "\nGetting the label committed to file: " << lowerFilePathOut << endl;
    auto lowerLabel = handler->GetLabel();
    cout << "Name: " + lowerLabel->GetLabel()->GetName() << endl;
    cout << "Id: " + lowerLabel->GetLabel()->GetId() << endl;
}
catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid label ID?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}
system("pause");

  1. 使用下列值取代原始程式碼中的預留位置值:

    預留位置
    <lower-label-id> 標籤識別碼,從上一個快速入門中的主控台輸出複製,例如: bb7ed207-046a-4caf-9826-647cff56b990 。 請確定其敏感度低於先前受保護的檔案卷標。
    <lower-output-file-path> 您想要儲存修改檔案的輸出檔案路徑。
    <lower-content-identifier> 內容的人類可讀取識別碼。

建置及測試應用程式

建置及測試用戶端應用程式。

  1. 使用 CTRL-SHIFT-B ( 建置解決方案 )來建置用戶端應用程式。 如果您沒有建置錯誤,請使用 F5 ( 開始偵錯 ) 來執行應用程式。

  2. 如果您的專案建置並成功執行,應用程式會在每次 SDK 呼叫您的 AcquireOAuth2Token() 方法時,提示輸入存取權杖。

  Non-Business : 87ba5c36-17cf-14793-bbc2-bd5b3a9f95cz
  Public : 83867195-f2b8-2ac2-b0b6-6bb73cb33afz
  General : f42a3342-8706-4288-bd31-ebb85995028z
  Confidential : 074e457c-5848-4542-9a6f-34a182080e7z
  Highly Confidential : f55c2dea-db0f-47cd-8520-a52e1590fb6z
  Press any key to continue . . .

  Applying Label ID f55c2dea-db0f-47cd-8520-a52e1590fb6z to c:\Test\Test.docx
  Committing changes


  Label committed to file: c:\Test\Test.docx
  Press any key to continue . . .

  Run the PowerShell script to generate an access token using the following values, then copy/paste it below:
  Set $authority to: https://login.windows.net/37f4583d-9985-4e7f-a1ab-71afd8b55ba0
  Set $resourceUrl to: https://aadrm.com
  Sign in with user account: user1@tenant.onmicrosoft.com
  Enter access token: <paste-access-token-here>
  Press any key to continue . . .

  Getting the label committed to file: c:\Test\Test_labeled.docx
  Name: Highly Confidential
  Id: f55c2dea-db0f-47cd-8520-a52e1590fb6z
  Press any key to continue . . . 

  Applying new Label ID f42a3342-8706-4288-bd31-ebb85995028z to c:\Test\Test_labeled.docx
  Please provide justification for downgrading a label:
  Need for sharing with wider audience.
  Committing changes

  Label committed to file: c:\Test\Test_downgraded.docx
  Press any key to continue . . .

  Getting the label committed to file: c:\Test\Test_downgraded.docx
  Name: General
  Id: f42a3342-8706-4288-bd31-ebb85995028z
  Press any key to continue . . .

請注意,如果從檔案中刪除標籤需要依據標籤原則的理由,則應該遵循類似的方法進行 DeleteLabel() 作業。 DeleteLabel() 函式會 mip::JustificationRequiredError 擲回例外狀況。 isDowngradeJustified 旗標應該在例外狀況處理中設定為 true,才能成功刪除標籤。