快速入門:設定並取得敏感度標籤(C++)

這個快速入門指南會教你如何使用更多 MIP 檔案 SDK。 透過使用你在前述快速入門中列出的敏感度標籤之一,你可以使用檔案處理程式來設定並取得檔案上的標籤。 檔案處理類別會提供各種操作,用於設定及取得支援檔案類型的標籤或保護。

先決條件

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

實作觀察者類別來監視 File 處理程式物件

就像你在應用程式初始化快速啟動中為檔案設定檔和引擎實作的觀察者一樣,現在你實作了一個檔案處理物件的觀察者類別。

透過 mip::FileHandler::Observer 擴充 SDK 類別,建立一個基本的檔案處理觀察器實作。 應用程式會實例化觀察者,並稍後用它來監控檔案處理程序。

  1. 開啟您在上一篇「快速入門:列出敏感度標籤(C++)》一文中處理過的 Visual Studio 解決方案。

  2. 為你的專案新增一個類別。 Visual Studio 會為您產生標頭檔 (.h) 和實作檔 (.cpp):

    • 方案總管中,再次以滑鼠右鍵按兩下項目節點,選取 [ 新增],然後選取 [ 類別]。
    • 在 [ 新增類別 ] 對話框中:
      • 在 [ 類別名稱] 欄位中,輸入 「filehandler_observer」。 請注意,根據您輸入的名稱,會自動填入 .h 檔案.cpp檔案 欄位。
      • 完成後,選取 [確定]
  3. Visual Studio 產生 .h 和 .cpp 檔案後,兩個檔案都會在編輯器分頁中開啟。 更新每個檔案以實作你的新觀察器類別:

    • 選取/刪除產生的 filehandler_observer 類別,以更新 “filehandler_observer.h”。 請勿 移除上一個步驟所產生的預處理器指示詞(#pragma、#include)。 然後在任何現有的預處理器指令之後,將下列原始碼複製/貼到檔案中。

      #include <memory>
      #include "mip/file/file_engine.h"
      #include "mip/file/file_handler.h"
      
      class FileHandlerObserver final : public mip::FileHandler::Observer {
      public:
         FileHandlerObserver() { }
         // Observer implementation
         void OnCreateFileHandlerSuccess(const std::shared_ptr<mip::FileHandler>& fileHandler, const std::shared_ptr<void>& context) override;
         void OnCreateFileHandlerFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;
         void OnCommitSuccess(bool committed, const std::shared_ptr<void>& context) override;
         void OnCommitFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;		
      };
      
    • 選取/刪除產生的 filehandler_observer 類別實作,以更新 “filehandler_observer.cpp”。 請勿 移除上一個步驟所產生的預處理器指示詞(#pragma、#include)。 然後在任何現有的預處理器指令之後,將下列原始碼複製/貼到檔案中。

      void FileHandlerObserver::OnCreateFileHandlerSuccess(const std::shared_ptr<mip::FileHandler>& fileHandler, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
         promise->set_value(fileHandler);
      }
      
      void FileHandlerObserver::OnCreateFileHandlerFailure(const std::exception_ptr & error, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
         promise->set_exception(error);
      }
      
      void FileHandlerObserver::OnCommitSuccess(bool committed, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<bool>>(context);
         promise->set_value(committed);
      }
      
      void FileHandlerObserver::OnCommitFailure(const std::exception_ptr & error, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<bool>>(context);
         promise->set_exception(error);
      }
      
  4. 可選擇性地使用F6(建置解決方案)執行測試編譯/連結,確認它建置成功後再繼續。

新增邏輯以設定並取得敏感度標籤

使用 File Engine 物件來新增邏輯,設定並取得檔案的敏感度標籤。

  1. 方案總管 中,打開專案中包含該方法實作main()的 .cpp 檔案。 預設名稱為包含它的專案的同一名稱,而這是在您建立專案時指定的。

  2. 在檔案頂端對應的現有指令後,加入以下 #includeusing 指令:

    #include "filehandler_observer.h" 
    #include "mip/file/file_handler.h" 
    
    using mip::FileHandler;
    
  3. main() 主體接近結尾處,也就是你在上一個快速入門中停下的位置,在 system("pause"); 之後、return 0; 之前插入以下程式碼:

    // Set up async FileHandler for input file operations
    string inputFilePath = "<input-file-path>";
    string actualFilePath = "<content-identifier>";
    std::shared_ptr<FileHandler> handler;
    try
    {
         auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
         auto handlerFuture = handlerPromise->get_future();
         engine->CreateFileHandlerAsync(
              inputFilePath,
              actualFilePath,                       
              true, 
              std::make_shared<FileHandlerObserver>(), 
              handlerPromise);
         handler = handlerFuture.get();
    }
    catch (const std::exception& e)
    {
         cout << "An exception occurred... did you specify a valid input file path?\n\n" << e.what() << "'\n";
         system("pause");
         return 1;
    }
    
    // Set a label on input file
    try
    {
         string labelId = "<label-id>";
         cout << "\nApplying Label ID " << labelId << " to " << inputFilePath << endl;
         mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
         handler->SetLabel(engine->GetLabelById(labelId), 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 filePathOut = "<output-file-path>";
    try
    {
     	cout << "Committing changes" << endl;
         auto commitPromise = std::make_shared<std::promise<bool>>();
         auto commitFuture = commitPromise->get_future();
         handler->CommitAsync(filePathOut, commitPromise);
     	if (commitFuture.get()) {
     		cout << "\nLabel committed to file: " << filePathOut << endl;
     	}
     	else {
     		cout << "Failed to label: " + filePathOut << 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
    actualFilePath = "<content-identifier>";
    try
    {
         auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
         auto handlerFuture = handlerPromise->get_future();
         engine->CreateFileHandlerAsync(
              filePathOut,
              actualFilePath,
              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 label from output file
    try
    {
         cout << "\nGetting the label committed to file: " << filePathOut << endl;
         auto label = handler->GetLabel();
         cout << "Name: " + label->GetLabel()->GetName() << endl;
         cout << "Id: " + label->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");
    
  4. main() 接近結尾時,找出在第一個快速入門中建立的應用程式關閉程式碼區塊,並將處理常式那一行取消註解:

    // Application shutdown. Null out profile and engine, call ReleaseAllResources();
    // Application may crash at shutdown if resources aren't properly released.
    profile = nullptr;
    engine = nullptr;
    handler = nullptr;
    mipContext = nullptr;
    
  5. 請以字串常數替換原始碼中的佔位值:

    佔位符 價值
    <輸入檔案路徑> 測試輸入檔的完整路徑,例如: "c:\\Test\\Test.docx"
    <內容識別碼> 內容的人類可讀識別碼。 例如:
    • 對於檔案,請使用 path\filename: "c:\Test\Test.docx"
    • 電子郵件請使用主旨:寄件人: "RE: Audit design:user1@contoso.com"
    <標籤識別碼> 例如:從上一個快速入門的控制台輸出中複製的敏感度標籤識別碼:"f42a3342-8706-4288-bd31-ebb85995028z"
    <輸出檔案路徑> 輸出檔案的完整路徑,該路徑是輸入檔案的標記副本,例如: "c:\\Test\\Test_labeled.docx"

建置及測試應用程式

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

  1. 使用 F6 (建置解決方案) 來建置用戶端應用程式。 如果建置沒有錯誤,就用 F5(開始除錯)來執行你的應用程式。

  2. 如果你的專案成功建置並執行,應用程式每次 SDK 呼叫你的 AcquireOAuth2Token() 方法時都會提示存取權杖。 如同您先前在「List sensitivity labels」快速入門中所做,請執行 PowerShell 指令碼,以便每次都使用為 $authority$resourceUrl 提供的值來取得權杖。

    Run the PowerShell script to generate an access token using the following values, then copy/paste it below:
    
    Sensitivity labels for your organization:
    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 074e457c-5848-4542-9a6f-34a182080e7z to c:\Test\Test.docx
    Committing changes
    
    Label committed to file: c:\Test\Test_labeled.docx
    Press any key to continue . . .
    
    Getting the label committed to file: c:\Test\Test_labeled.docx
    Name: Confidential
    Id: 074e457c-5848-4542-9a6f-34a182080e7z
    Press any key to continue . . .
    

請打開輸出檔並目視檢視文件的資訊保護設定,以驗證標籤的應用。

備註

如果你在標記 Office 文件,但沒有用發出存取權杖的 Microsoft Entra 租戶帳號登入(且敏感度標籤已設定),你可能會被要求先登入才能開啟標籤文件。

下一步