次の方法で共有


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

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

[前提条件]

まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。

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

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

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

  1. 前の「クイック スタート: 秘密度ラベルの一覧表示 (C++)」の記事で作業した Visual Studio ソリューションを開きます。

  2. プロジェクトに新しいクラスを追加すると、ヘッダー/.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. 文字列定数を使用して、ソース コード内のプレースホルダーの値を次のように置き換えます。

    プレースホルダー 価値
    <入力ファイルパス> テスト入力ファイルへの完全なパス (例: "c:\\Test\\Test.docx")。
    <コンテンツ識別子> 人が判読できるコンテンツの識別子。 例えば:
    • ファイルの場合は、path\filename を検討してください。 "c:\Test\Test.docx"
    • 電子メールの場合は、subject:sender を検討してください。 "RE: Audit design:user1@contoso.com"
    <ラベル-ID> 前のクイック スタートのコンソール出力からコピーされた秘密度ラベル ID (例: "f42a3342-8706-4288-bd31-ebb85995028z")。
    <出力ファイルパス> 出力ファイルへの完全なパス。入力ファイルのラベル付きコピーになります (例: "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 . . .
    

出力ファイルを開き、ドキュメントの情報保護設定を視覚的に調べることで、ラベルのアプリケーションを確認できます。

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