共用方式為


Microsoft資訊保護檔案 SDK - 降低檔案上敏感度標籤的動作理由 (C#)

此快速入門指南說明在標籤政策要求提供理由時,如何處理降級標籤操作。在這裡,我們將使用 IFileHandler 介面來變更檔案的標籤。 如需進一步的詳細數據,請參閱 API 參考

先決條件

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

  • 完成 快速入門:設定/取得敏感度標籤(C#) 以建置入門 Visual Studio 解決方案,以列出組織的敏感度標籤,以及設定和讀取檔案中的敏感度捲標。 本「如何 - 降級/移除需要理由的 C# 標籤」快速入門指南是基於上一個快速入門指南。
  • 選擇性檢閱 MIP SDK 概念中的檔案處理程式概念。

新增邏輯以將較低標籤設定為受保護的檔案

使用 File 處理程式物件,新增邏輯以在檔案上設定敏感度標籤。

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

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

  3. <label-id> 從先前快速入門中的值更新為一個敏感度標籤,該標籤需要提供降低敏感度的理由。 在本快速入門執行期間,我們會先設定此標籤,然後嘗試在進一步的步驟中透過代碼段將其降低。

  4. Main() 主體的末尾,於 Console.ReadKey() 下方和應用程式關機區塊上方(即您在上一個快速入門中停下的位置)插入以下程式碼。

    //Set paths and label ID
    string lowerInput = actualOutputFilePath;
    string lowerActualInput = lowerInput;
    string newLabelId = "<new-label-id>";
    string lowerOutput = "<downgraded-labled-output>";
    string lowerActualOutput = lowerOutput;
    
    //Create a file handler for that file
    var downgradeHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(lowerInput, lowerActualInput, true)).Result;
    
    //Set Labeling Options
    LabelingOptions options = new LabelingOptions()
    {
        AssignmentMethod = AssignmentMethod.Standard
    };
    
    try
    {
        //Try to set new label
        downgradeHandler.SetLabel(fileEngine.GetLabelById(newLabelId), options, new ProtectionSettings());
    }
    
    catch (Microsoft.InformationProtection.Exceptions.JustificationRequiredException)
    {
        //Request justification from user
        Console.Write("Please provide justification for downgrading a label: ");
        string justification = Console.ReadLine();
    
        options.IsDowngradeJustified = true;
        options.JustificationMessage = justification;
    
        //Set new label
        downgradeHandler.SetLabel(fileEngine.GetLabelById(newLabelId), options, new ProtectionSettings());
    }
    
    // Commit changes, save as outputFilePath
    var downgradedResult = Task.Run(async () => await downgradeHandler.CommitAsync(lowerActualOutput)).Result;
    
    // Create a new handler to read the labeled file metadata
    var commitHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(lowerOutput, lowerActualOutput, true)).Result;
    
    // Get the label from output file
    var newContentLabel = commitHandler.Label;
    Console.WriteLine(string.Format("Getting the new label committed to file: {0}", lowerOutput));
    Console.WriteLine(string.Format("File Label: {0} \r\nIsProtected: {1}", newContentLabel.Label.Name, newContentLabel.IsProtectionAppliedFromLabel.ToString()));
    Console.WriteLine("Press a key to continue.");
    Console.ReadKey();
    
    
  5. 在Main() 結尾,尋找在上一個快速入門中建立的應用程式關機區塊,並新增下列處理程式行來釋放資源。

    downgradeHandler = null;
    commitHandler = null;
    
  6. 使用下列值取代原始碼中的佔位元值:

    佔位符 價值
    <降級標記輸出> 您要儲存修改檔案的輸出檔案路徑。
    <新標籤識別碼> 從上一個快速入門中的控制台輸出複製的範本標識碼,例如: bb7ed207-046a-4caf-9826-647cff56b990。 請確定其敏感度低於先前受保護的檔案標籤。

建置及測試應用程式

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

  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_labeled.docx
  Name: Confidential
  IsProtected: True
  Press any key to continue . . .

  Please provide justification for downgrading a label: Lower label approved.
  Getting the new label committed to file: c:\Test\Test_downgraded.docx
  File Label: General
  IsProtected: False
  Press a key to continue.

請注意,相同的方法亦適用於 DeleteLabel() 操作,如果依據標籤政策,刪除檔案中的標籤需要理由。 DeleteLabel() 函式會擲回 JustificationRequiredException 例外狀況,並且在處理例外狀況時 IsDowngradeJustified 旗標應設定為 true,這樣才能成功刪除標籤。