快速入門:設定並取得敏感度標籤 (C#)

本快速入門說明如何使用更多 MIP 檔案 SDK。 使用您在上一個快速入門中列出的其中一個敏感度標籤,您可以使用檔案處理常式來設定/取得檔案上的標籤。 File handler 類別會針對支援的檔案類型,公開設定/取得標籤或保護的各種作業。

必要條件

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

新增邏輯以設定並取得敏感度標籤

使用檔案引擎物件,新增邏輯以在檔案上設定並取得敏感度標籤。

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

  2. 在本文結尾 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();
    
  3. 在第一個快速入門中尋找建立的應用程式關機區塊, Main() 並取消批註處理常式行:

    // Application Shutdown
    handler = null;
    fileEngine = null;
    fileProfile = null;
    mipContext = null;
    
  4. 使用下列值取代原始程式碼中的預留位置值:

    預留位置
    <input-file-path> 測試輸入檔的完整路徑,例如: c:\\Test\\Test.docx
    <label-id> 從上一個快速入門中的主控台輸出複製的敏感度標籤識別碼,例如: f42a3342-8706-4288-bd31-ebb85995028z
    <output-file-path> 輸出檔案的完整路徑,這會是輸入檔的標籤複本,例如: c:\\Test\\Test_labeled.docx

建置及測試應用程式

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

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

  2. 如果您的專案建置並成功執行, 應用程式可能會在 每次 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 租使用者帳戶登入(且已設定敏感度標籤),則系統可能會提示您登入,才能開啟標示的檔。