共用方式為


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.