Microsoft 資訊保護 SDK - 檔案 SDK 重新發佈快速入門 (C#)

概觀

如需此案例的概觀及其使用位置,請參閱 在 MIP SDK 中重新發佈。

必要條件

如果您尚未完成,請務必先完成下列必要條件,再繼續進行:

新增邏輯以編輯並重新發佈受保護的檔案

  1. 開啟您在先前的「快速入門:設定/取得敏感度標籤(C#)》一文中建立的 Visual Studio 解決方案。

  2. 使用 方案總管,在您的專案中開啟包含 方法實作的 Main() .cs 檔案。 它預設為與您在專案建立期間指定的專案相同名稱。

  3. 在本文結尾 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();
}
  1. 在 Main() 結尾時,尋找在上一個快速入門中建立的應用程式關機區塊,並新增下列處理常式行來釋放資源。

        protectedFileHandler = null;
        protectionHandler = null;
    
  2. 使用下列值取代原始程式碼中的預留位置值:

    預留位置
    <protected-file-path> 來自上一個快速入門的受保護檔案。
    <reprotected-file-path> 要重新發行之已修改檔案的輸出檔案路徑。

建置及測試應用程式

建置及測試用戶端應用程式。

  1. 使用 CTRL-SHIFT-B ( 建置解決方案 )來建置用戶端應用程式。 如果您沒有建置錯誤,請使用 F5 ( 開始偵錯 ) 來執行應用程式。

  2. 如果您的專案建置並成功執行,則每次 SDK 呼叫方法 AcquireToken() 時,應用程式 可能會 提示使用 Microsoft 驗證程式庫 (MSAL) 進行驗證。 如果快取的認證已經存在,系統將不會提示您登入並查看標籤清單,後面接著已套用標籤和已修改檔案的資訊。

  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.