概要
このシナリオの概要と使用できる場所については、 MIP SDK での再発行に関するページを参照してください。
[前提条件]
まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。
- 完全な クイック スタート: 秘密度ラベルを設定/取得する (C#) 最初に、組織の秘密度ラベルを一覧表示するスターター Visual Studio ソリューションを構築し、ファイルとの間で秘密度ラベルを設定および読み取ります。 この「方法 - 保護されたファイルを再発行する - C#」クイック スタートは、前のクイック スタートに基づいています。
- 必要に応じて、MIP SDK の概念で ファイル ハンドラー を確認します。
- 必要に応じて、MIP SDK の概念で 保護ハンドラー を確認します。
保護されたファイルを編集および再発行するロジックを追加する
前の「クイック スタート: 秘密度ラベルの設定/取得 (C#)」の記事で作成した Visual Studio ソリューションを開きます。
ソリューション エクスプローラーを使用して、
Main()メソッドの実装を含む.cs ファイルをプロジェクトで開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。Main()本文の最後の辺り、Console.ReadKey()の下、アプリケーションのシャットダウン ブロックの上 (前のクイックスタートで終了した場所) に次のコードを挿入します。
string protectedFilePath = "<protected-file-path>" // Originally protected file's path from previous quickstart.
//Create a fileHandler for consumption for the Protected File.
var protectedFileHandler = Task.Run(async () =>
await fileEngine.CreateFileHandlerAsync(protectedFilePath,// inputFilePath
protectedFilePath,// actualFilePath
false, //isAuditDiscoveryEnabled
null)).Result; // fileExecutionState
// Store protection handler from file
var protectionHandler = protectedFileHandler.Protection;
//Check if the user has the 'Edit' right to the file
if (protectionHandler.AccessCheck("Edit"))
{
// Decrypt file to temp path
var tempPath = Task.Run(async () => await protectedFileHandler.GetDecryptedTemporaryFileAsync()).Result;
/*
Your own application code to edit the decrypted file belongs here.
*/
/// Follow steps below for re-protecting the edited file. ///
// Create a new file handler using the temporary file path.
var republishHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(tempPath, tempPath, false)).Result;
// Set protection using the ProtectionHandler from the original consumption operation.
republishHandler.SetProtection(protectionHandler);
// New file path to save the edited file
string reprotectedFilePath = "<reprotected-file-path>" // New file path for saving reprotected file.
// Write changes
var reprotectedResult = Task.Run(async () => await republishHandler.CommitAsync(reprotectedFilePath)).Result;
var protectedLabel = protectedFileHandler.Label;
Console.WriteLine(string.Format("Originally protected file: {0}", protectedFilePath));
Console.WriteLine(string.Format("File LabelID: {0} \r\nProtectionOwner: {1} \r\nIsProtected: {2}",
protectedLabel.Label.Id,
protectedFileHandler.Protection.Owner,
protectedLabel.IsProtectionAppliedFromLabel.ToString()));
var reprotectedLabel = republishHandler.Label;
Console.WriteLine(string.Format("Reprotected file: {0}", reprotectedFilePath));
Console.WriteLine(string.Format("File LabelID: {0} \r\nProtectionOwner: {1} \r\nIsProtected: {2}",
reprotectedLabel.Label.Id,
republishHandler.Protection.Owner,
reprotectedLabel.IsProtectionAppliedFromLabel.ToString()));
Console.WriteLine("Press a key to continue.");
Console.ReadKey();
}
Main() の終わりに向かって、前のクイック スタートで作成したアプリケーションのシャットダウン ブロックを見つけ、以下のハンドラー行を追加してリソースを解放します。
protectedFileHandler = null; protectionHandler = null;次の値を使用して、ソース コード内のプレースホルダーの値を置き換えます。
プレースホルダー 価値 <保護されたファイルパス> 前のクイック スタートから保護されたファイル。 <再保護されたファイルパス> 再発行する変更されたファイルの出力ファイル パス。
アプリケーションのビルドとテスト
クライアント アプリケーションをビルドしてテストします。
Ctrl + Shift + B キー ([ソリューションのビルド]) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、F5 (デバッグの開始) を使用してアプリケーションを実行します。
プロジェクトが正常にビルドされて実行された場合、SDK から メソッドが呼び出されるたびに、アプリケーションで Microsoft 認証ライブラリ (MSAL) を使用した認証を求めるダイアログが表示される "場合があります"。
AcquireToken()キャッシュされた資格情報が既に存在する場合は、サインインしてラベルの一覧を表示し、その後に適用されたラベルと変更されたファイルに関する情報を表示するように求められることはありません。
Personal : 73c47c6a-eb00-4a6a-8e19-efaada66dee6
Public : 73254501-3d5b-4426-979a-657881dfcb1e
General : da480625-e536-430a-9a9e-028d16a29c59
Confidential : 569af77e-61ea-4deb-b7e6-79dc73653959
Highly Confidential : 905845d6-b548-439c-9ce5-73b2e06be157
Press a key to continue.
Getting the label committed to file: C:\Test\Test_protected.docx
File Label: Confidential
IsProtected: True
Press a key to continue.
Originally protected file: C:\Test\Test_protected.docx
File LabelID: 569af77e-61ea-4deb-b7e6-79dc73653959
ProtectionOwner: User1@Contoso.OnMicrosoft.com
IsProtected: True
Reprotected file: C:\Test\Test_reprotected.docx
File LabelID: 569af77e-61ea-4deb-b7e6-79dc73653959
ProtectionOwner: User1@Contoso.OnMicrosoft.com
IsProtected: True
Press a key to continue.