このクイック スタートでは、MIP ファイル SDK をさらに使用する方法について説明します。 前のクイック スタートで示した秘密度ラベルのいずれかを使用して、ファイル ハンドラーを使用してファイルのラベルを設定または取得します。 File ハンドラー クラスは、サポートされているファイルの種類に対して、ラベルの設定/取得、または保護のためのさまざまな操作を公開します。
前提条件
まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。
- まず「クイックスタート: 秘密度ラベルの一覧表示 (C#)」を完了して、Visual Studio のスターター ソリューションを構築し、組織の秘密度ラベルを一覧表示します。 この「秘密度ラベルの設定と取得」クイックスタートは、先行するクイックスタートをベースにしています。
- 必要に応じて、 MIP SDK の概念でファイル ハンドラー を確認します。
秘密度ラベルを設定および取得するロジックを追加する
ファイル エンジン オブジェクトを使用して、ファイルに秘密度ラベルを設定して取得するロジックを追加します。
ソリューション エクスプローラーを使用して、Main()' メソッドの実装を含むプロジェクト内の.cs ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。
Main()本文の末尾に、var fileEngine =した後、//Application Shutdownコメントの上に、次のコードを挿入します。//Set paths and label ID string inputFilePath = "<input-file-path>"; string actualFilePath = inputFilePath; string labelId = "<label-id>"; string outputFilePath = "<output-file-path>"; string actualOutputFilePath = outputFilePath; //Create a file handler for that file //Note: the 2nd inputFilePath is used to provide a human-readable content identifier for admin auditing. var handler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(inputFilePath, actualFilePath, true)).Result; //Set Labeling Options LabelingOptions labelingOptions = new LabelingOptions() { AssignmentMethod = AssignmentMethod.Standard }; // Set a label on input file handler.SetLabel(fileEngine.GetLabelById(labelId), labelingOptions, new ProtectionSettings()); // Commit changes, save as outputFilePath var result = Task.Run(async () => await handler.CommitAsync(outputFilePath)).Result; // Create a new handler to read the labeled file metadata var handlerModified = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(outputFilePath, actualOutputFilePath, true)).Result; // Get the label from output file var contentLabel = handlerModified.Label; Console.WriteLine(string.Format("Getting the label committed to file: {0}", outputFilePath)); Console.WriteLine(string.Format("File Label: {0} \r\nIsProtected: {1}", contentLabel.Label.Name, contentLabel.IsProtectionAppliedFromLabel.ToString())); Console.WriteLine("Press a key to continue."); Console.ReadKey();Main()の最後に、最初のクイック スタートで作成されたアプリケーション シャットダウン ブロックを見つけて、ハンドラー行のコメントを解除します。// Application Shutdown handler = null; fileEngine = null; fileProfile = null; mipContext = null;次の値を使用して、ソース コード内のプレースホルダーの値を置き換えます。
プレースホルダー 価値 <input-file-path> テスト入力ファイルへの完全なパス (例: c:\\Test\\Test.docx)。<label-id> 前のクイック スタートのコンソール出力からコピーされた秘密度ラベル ID (例: f42a3342-8706-4288-bd31-ebb85995028z)。<output-file-path> 出力ファイルへの完全なパス。入力ファイルのラベル付きコピーになります (例: c:\\Test\\Test_labeled.docx)。
アプリケーションのビルドとテスト
クライアント アプリケーションをビルドしてテストします。
Ctrl -SHIFT-B (ソリューションのビルド) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、F5 (デバッグの開始) を使用してアプリケーションを実行します。
プロジェクトが正常にビルドされて実行されると、SDK が
AcquireToken()メソッドを呼び出すたびに、ADAL 経由で認証を求められる場合があります。 キャッシュされた資格情報が既に存在する場合は、サインインしてラベルの一覧を表示し、その後に適用されたラベルと変更されたファイルに関する情報を表示するように求められることはありません。
Personal : 73c47c6a-eb00-4a6a-8e19-efaada66dee6
Public : 73254501-3d5b-4426-979a-657881dfcb1e
General : da480625-e536-430a-9a9e-028d16a29c59
Confidential : 569af77e-61ea-4deb-b7e6-79dc73653959
Recipients Only (C) : d98c4267-727b-430e-a2d9-4181ca5265b0
All Employees (C) : 2096f6a2-d2f7-48be-b329-b73aaa526e5d
Anyone (not protected) (C) : 63a945ec-1131-420d-80da-2fedd15d3bc0
Highly Confidential : 905845d6-b548-439c-9ce5-73b2e06be157
Recipients Only : 05ee72d9-1a75-441f-94e2-dca5cacfe012
All Employees : 922b06ef-044b-44a3-a8aa-df12509d1bfe
Anyone (not protected) : c83fc820-961d-40d4-ba12-c63f72a970a3
Press a 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
File Label: Confidential
IsProtected: false
Press any key to continue.
出力ファイルを開き、ドキュメントの情報保護設定を視覚的に調べることで、ラベルのアプリケーションを確認できます。
注
Office ドキュメントにラベルを付けるが、アクセス トークンが取得された Microsoft Entra テナントのアカウントを使用してサインインしていない場合 (および秘密度ラベルが構成されている場合)、ラベル付けされたドキュメントを開く前にサインインするように求められる場合があります。