クイックスタート: 秘密度ラベルの設定と取得 (C++)

このクイックスタートでは、MIP File SDK をさらに利用する方法について説明します。 先行するクイックスタートで一覧表示した秘密度ラベルのいずれかを用い、ファイル ハンドラーを使用して、ファイルのラベルを設定および取得します。 ファイル ハンドラー クラスでは、サポートされているファイルの種類に対して、ラベルの設定と取得、または保護のためのさまざまな操作が公開されています。

前提条件

先に進む前に、次の前提条件をまだ実行していない場合は完了してください。

ファイル ハンドラー オブジェクトを監視するためのオブザーバー クラスを実装する

アプリケーションの初期化に関するクイックスタートで (ファイル プロファイルとエンジン用に) 実装したオブザーバーと同様、ファイル ハンドラー オブジェクト用のオブザーバー クラスを実装します。

SDK の mip::FileHandler::Observer クラスを拡張して、ファイル ハンドラー オブザーバーの基本的な実装を作成します。 このオブザーバーは、ファイル ハンドラーの操作を監視するために後でインスタンス化して使用します。

  1. 先行する "秘密度ラベルの一覧表示に関するクイックスタート (C++)" の記事で作成した Visual Studio ソリューションを開きます。

  2. 新しいクラスをプロジェクトに追加します。これにより、header/.h と implementation/.cpp の両方のファイルが生成されます。

    • ソリューション エクスプローラーでもう一度プロジェクト ノードを右クリックし、[追加][クラス] の順に選択します。
    • [クラスの追加] ダイアログで、以下の操作を行います。
      • [クラス名] フィールドに「filehandler_observer」と入力します。 [.h ファイル][.cpp ファイル] の両方のフィールドが、入力した名前に基づいて自動的に設定されることに注意してください。
      • 完了したら、[OK] ボタンをクリックします。
  3. クラスの .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 キー ([ソリューションのビルド]) を使用してソリューションのテスト コンパイルとリンクを実行し、正しくビルドされていることを確認します。

秘密度ラベルを設定および取得するロジックを追加する

ファイル エンジン オブジェクトを使用して、ファイルに対して秘密度ラベルを設定および取得するロジックを追加します。

  1. ソリューション エクスプローラーを使用して、main() メソッドの実装を含むプロジェクトの .cpp ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。

  2. ファイルの先頭に、次の #include および using ディレクティブを対応する既存のディレクティブの下に追加します。

    #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 " << filePathIn << endl;
         mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
         handler->SetLabel(engine->GetLabelById(labelId), labelingOptions, new 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. 次の文字列定数を使用して、ソース コードのすべてのプレースホルダー値を置き換えます。

    プレースホルダー Value
    <input-file-path> テスト入力ファイルの完全なパス (例: "c:\\Test\\Test.docx")。
    <content-identifier> 人が判読できるコンテンツの識別子。 例:
    • ファイルの場合 (path\filename 形式など): "c:\Test\Test.docx"
    • メールの場合 (subject:sender 形式など): "RE: Audit design:user1@contoso.com"
    <label-id> 先行するクイックスタートのコンソール出力からコピーされた秘密度ラベル ID (例: "f42a3342-8706-4288-bd31-ebb85995028z")。
    <output-file-path> 出力ファイル、つまり入力ファイルのラベル付きコピーの完全なパス (例: "c:\\Test\\Test_labeled.docx")。

アプリケーションのビルドとテスト

クライアント アプリケーションをビルドしてテストします。

  1. F6 キー ([ソリューションのビルド]) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、F5 キー ([デバッグの開始]) を使用してアプリケーションを実行します。

  2. プロジェクトがビルドされて正常に実行された場合、SDK が AcquireOAuth2Token() メソッドを呼び出すたびに、アプリケーションからアクセス トークンを求められます。 既に "秘密度ラベルの一覧表示" のクイックスタートで行ったように、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 . . .
    

出力ファイルを開き、ドキュメントの情報保護設定を目視でチェックすることで、ラベルの適用を確認できます。

Note

Office ドキュメントにラベルを付ける際、アクセス トークンが取得された (かつ秘密度ラベルが構成されている) Microsoft Entra テナントのアカウントを使用してサインインしていない場合は、ラベル付きドキュメントを開く前にサインインを求められる場合があります。