File SDK では、他のファイルの種類と同じ方法で.msg ファイルのラベル付け操作がサポートされます。ただし、SDK ではアプリケーションで MSG 機能フラグを有効にする必要があります。 このクイック スタートでは、このフラグを設定する方法について説明します。
前に説明したように、 mip::FileEngine をインスタンス化するには、 mip::FileEngineSettings 設定オブジェクトが必要です。 アプリケーションは、 FileEngineSettings を使用して、特定のインスタンスのカスタム設定を渡します。
mip::FileEngineSettingsのCustomSettings プロパティは、.msg ファイルの処理を有効にするenable_msg_file_type フラグを設定します。
[前提条件]
まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。
- 完全なクイック スタート: 最初に、スターター Visual Studio ソリューションを構築する File SDK アプリケーション初期化 (C++) を実行します。 この"方法 - 電子メール メッセージ .msg ファイルを処理する (C++)" クイック スタートは、前のクイック スタートに基づいています。
- 「クイックスタート: 秘密度ラベルの一覧表示 (C++)」を確認します。
- クイック スタート: 秘密度ラベルの設定/取得 (C++) を確認します。
- 電子メール ファイル MIP SDK の概念を確認します。
- 必要に応じて、 MIP SDK の概念でファイル エンジン を確認します。
- 必要に応じて、 MIP SDK の概念でファイル ハンドラー を確認します。
前提条件の実装手順
前の「クイック スタート: クライアント アプリケーションの初期化 (C++)」の記事で作成した Visual Studio ソリューションを開きます。
クイック スタート: 秘密度ラベルの一覧表示 (C++) で説明されているように、アクセス トークンを生成する PowerShell スクリプトを作成します。
「クイック スタート: 秘密度ラベルの設定/取得 (C++)」で説明されているように、
mip::FileHandlerを監視するオブザーバー クラスを実装します。
enable_msg_file_typeを設定し、File SDK を使用してファイル.msgラベルを付ける
次のファイル エンジン構築コードを追加して、 enable_msg_file_type フラグを設定し、ファイル エンジンを使用して.msg ファイルにラベルを付けます。
ソリューション エクスプローラーで、
main()メソッドの実装を含むプロジェクト内の.cpp ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。ファイルの先頭にある、対応する既存のディレクティブの後に、次の #include と using ディレクティブを追加します。
#include "filehandler_observer.h" #include "mip/file/file_handler.h" #include <iostream> using mip::FileHandler; using std::endl;前のクイック スタートから、
main()関数の実装を削除します。main()本文内に、次のコードを挿入します。 次のコード ブロックでは、ファイル エンジンの作成によってenable_msg_file_typeフラグが設定されます。mip::FileHandlerファイル エンジンを使用して作成されたオブジェクトは、.msg ファイルを処理できます。
int main()
{
// Construct/initialize objects required by the application's profile object
ApplicationInfo appInfo { "<application-id>", // ApplicationInfo object (App ID, name, version)
"<application-name>",
"1.0"
};
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(appInfo,
"mip_data",
mip::LogLevel::Trace,
false,
mip::CacheStorageType::OnDisk);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
auto profileObserver = make_shared<ProfileObserver>(); // Observer object
auto authDelegateImpl = make_shared<AuthDelegateImpl>("<application-id>"); // Authentication delegate object (App ID)
auto consentDelegateImpl = make_shared<ConsentDelegateImpl>(); // Consent delegate object
// Construct/initialize profile object
FileProfile::Settings profileSettings(mMipContext, mip::CacheStorageType::OnDisk,
consentDelegateImpl, profileObserver);
// Set up promise/future connection for async profile operations; load profile asynchronously
auto profilePromise = make_shared<promise<shared_ptr<FileProfile>>>();
auto profileFuture = profilePromise->get_future();
try
{
mip::FileProfile::LoadAsync(profileSettings, profilePromise);
}
catch (const std::exception& e)
{
std::cout << "An exception occurred. Are the Settings and ApplicationInfo objects populated correctly?\n\n"<< e.what() << "'\n";
system("pause");
return 1;
}
auto profile = profileFuture.get();
// Construct/initialize engine object
FileEngine::Settings engineSettings(
mip::Identity("<engine-account>"), // Engine identity (account used for authentication)
authDelegateImpl, // Token acquisition implementation
"<engine-state>", // User-defined engine state
"en-US"); // Locale (default = en-US)
//Set enable_msg_file_type flag as true
std::vector<std::pair<string, string>> customSettings;
customSettings.emplace_back(mip::GetCustomSettingEnableMsgFileType(), "true");
engineSettings.SetCustomSettings(customSettings);
// Set up promise/future connection for async engine operations; add engine to profile asynchronously
auto enginePromise = make_shared<promise<shared_ptr<FileEngine>>>();
auto engineFuture = enginePromise->get_future();
profile->AddEngineAsync(engineSettings, enginePromise);
std::shared_ptr<FileEngine> engine;
try
{
engine = engineFuture.get();
}
catch (const std::exception& e)
{
cout << "An exception occurred... is the access token incorrect/expired?\n\n"<< e.what() << "'\n";
system("pause");
return 1;
}
//Set file paths
string inputFilePath = "<input-file-path>"; //.msg file to be labeled
string actualFilePath = inputFilePath;
string outputFilePath = "<output-file-path>"; //labeled .msg file
string actualOutputFilePath = outputFilePath;
//Create a file handler for original file
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);
auto fileHandler = handlerFuture.get();
//List labels available to the user
// Use mip::FileEngine to list all labels
auto labels = engine->ListSensitivityLabels();
// Iterate through each label, first listing details
for (const auto& label : labels) {
cout << label->GetName() << " : " << label->GetId() << endl;
// get all children for mip::Label and list details
for (const auto& child : label->GetChildren()) {
cout << "-> " << child->GetName() << " : " << child->GetId() << endl;
}
}
string labelId = "<labelId-id>"; //set a label ID to use
// Labeling requires a mip::LabelingOptions object.
// Review API ref for more details. The sample implies that the file was labeled manually by a user.
mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
// Resolve the label ID to a mip::Label, then apply it.
auto label = engine->GetLabelById(labelId);
fileHandler->SetLabel(label, labelingOptions, mip::ProtectionSettings());
// Commit changes, save as outputFilePath
auto commitPromise = std::make_shared<std::promise<bool>>();
auto commitFuture = commitPromise->get_future();
if(fileHandler->IsModified())
{
fileHandler->CommitAsync(outputFilePath, commitPromise);
}
if (commitFuture.get()) {
cout << "\n Label applied to file: " << outputFilePath << endl;
}
else {
cout << "Failed to label: " + outputFilePath << endl;
return 1;
}
// Create a new handler to read the label
auto msgHandlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
auto msgHandlerFuture = msgHandlerPromise->get_future();
engine->CreateFileHandlerAsync(inputFilePath,
actualFilePath,
true,
std::make_shared<FileHandlerObserver>(),
msgHandlerPromise);
auto msgFileHandler = msgHandlerFuture.get();
cout << "Original file: " << inputFilePath << endl;
cout << "Labeled file: " << outputFilePath << endl;
cout << "Label applied to file : "
<< msgFileHandler->GetLabel()->GetLabel()->GetName()
<< endl;
// Application shutdown. Null out profile, engine, handler.
// Application may crash at shutdown if resources aren't properly released.
msgFileHandler = nullptr;
fileHandler = nullptr;
engine = nullptr;
profile = nullptr;
mMipContext->ShutDown();
mMipContext = nullptr;
return 0;
}
ファイル操作の詳細については、「 ファイル ハンドラーの概念」を参照してください。
ソース コードのプレースホルダーの値を次の値に置き換えます。
プレースホルダー 価値 <application-id> Microsoft Entra テナントに登録されているアプリケーション ID (例: 00001111-aaaa-2222-bbbb-3333cccc4444)。<エンジンアカウント> エンジン ID に使用されるアカウント (例: user@tenant.onmicrosoft.com)。<エンジン状態> ユーザー定義アプリケーションの状態 (例: My engine state)。<入力ファイルパス> テスト入力メッセージ ファイルへの完全なパス (例: c:\\Test\\message.msg)。<出力ファイルパス> 出力ファイルへの完全なパス。入力ファイルのラベル付きコピーです (例: c:\\Test\\message_labeled.msg)。<ラベル-ID> ListSensitivityLabelsを使用して取得されるラベル ID (例:667466bf-a01b-4b0a-8bbf-a79a3d96f720)。
アプリケーションのビルドとテスト
F6 (ソリューションのビルド) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、 F5 (デバッグの開始) を使用してアプリケーションを実行します。