このクイック スタートでは、より多くの MIP ファイル SDK を使用する方法について説明します。 前のクイック スタートで示した秘密度ラベルのいずれかを使用して、ファイル ハンドラーを使用してファイルのラベルを設定および取得します。 ファイル ハンドラー クラスは、サポートされているファイルの種類のラベルまたは保護を設定および取得するためのさまざまな操作を公開します。
[前提条件]
まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。
- クイック スタート: 秘密度ラベルを一覧表示する (C++)。 最初に、このクイック スタートを完了します。 組織の秘密度ラベルを一覧表示するスターター Visual Studio ソリューションを構築します。 この「秘密度ラベルを設定して取得する」クイックスタートは、前のクイックスタートを基にしています。
- 必要に応じて、 MIP SDK の概念でファイル ハンドラー を確認します。
ファイル ハンドラー オブジェクトを監視するオブザーバー クラスを実装する
アプリケーション初期化クイック スタートでファイル プロファイルとエンジンに実装したオブザーバーと同様に、ファイル ハンドラー オブジェクトのオブザーバー クラスを実装します。
SDK の mip::FileHandler::Observer クラスを拡張して、ファイル ハンドラー オブザーバーの基本的な実装を作成します。 アプリケーションはオブザーバーをインスタンス化し、後でそれを使用してファイル ハンドラー操作を監視します。
前の「クイック スタート: 秘密度ラベルの一覧表示 (C++)」の記事で作業した Visual Studio ソリューションを開きます。
新しいクラスをプロジェクトに追加します。 Visual Studioでは、ヘッダー/.h ファイルと implementation/.cpp ファイルの両方が生成されます。
- ソリューション エクスプローラーで、もう一度プロジェクト ノードを右クリックし、[追加] を選択し、[クラス] を選択します。
- [ クラスの追加 ] ダイアログで、次の手順を実行します。
- [ クラス名] フィールドに「filehandler_observer」と入力します。 入力した名前に基づいて、 .h ファイル フィールドと .cpp ファイル フィールドの両方が自動的に設定されます。
- 終わったら、 [OK] を選択します。
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); }
必要に応じて、F6 (ソリューションのビルド) を使用して、ソリューションのテスト コンパイル/リンクを実行し、続行する前に正常にビルドされることを確認します。
秘密度ラベルを設定および取得するロジックを追加する
ファイル エンジン オブジェクトを使用して、ファイルに秘密度ラベルを設定して取得するロジックを追加します。
ソリューション エクスプローラーで、
main()メソッドの実装を含むプロジェクト内の.cpp ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。ファイルの先頭にある対応する既存のディレクティブの後に、次の
#includeおよびusingディレクティブを追加します。#include "filehandler_observer.h" #include "mip/file/file_handler.h" using mip::FileHandler;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");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;文字列定数を使用して、ソース コードのプレースホルダー値を次のように置き換えます。
プレースホルダー 価値 <入力ファイルパス> テスト入力ファイルへの完全なパス (例: "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")。- ファイルの場合は、path\filename を使用します。
アプリケーションのビルドとテスト
クライアント アプリケーションをビルドしてテストします。
F6 (ソリューションのビルド) を使用して、クライアント アプリケーションをビルドします。 ビルドにエラーがない場合は、F5 (デバッグの開始) を使用してアプリケーションを実行します。
プロジェクトが正常にビルドされて実行されると、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 テナントのアカウントを使用してサインインしていない場合 (および秘密度ラベルが構成されている場合)、ラベル付けされたドキュメントを開く前にサインインするように求められる場合があります。