本快速入門說明如何使用更多 MIP 檔案 SDK。 使用您在上一個快速入門中列出的其中一個敏感度標籤,您可以使用檔案處理程式來設定/取得檔案上的標籤。 File handler 類別會針對支援的檔類型,公開設定/取得標籤或保護的各種作業。
先決條件
如果您尚未完成,請務必先完成下列必要條件,再繼續進行:
- 完成 快速入門:先列出敏感度標籤 (C#) 以建置入門 Visual Studio 解決方案,以列出組織的敏感度標籤。 本「設定並取得敏感度標籤」快速入門是以「上述快速入門」為基礎。
- 選擇性:檢閱 MIP SDK 概念中的檔案處理程式 。
新增邏輯以設定並取得敏感度標籤
使用檔案引擎物件,新增邏輯以在檔案上設定並取得敏感度標籤。
使用 方案總管,在您的專案中開啟包含 Main() 方法實作的 .cs 檔案。 它的預設名稱是包含它的專案名稱,這是您在建立專案時指定的。
在
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();在
Main()的結尾處,找到在第一次快速入門中建立的應用程式關閉區塊,並將處理程式行取消註解:// Application Shutdown handler = null; fileEngine = null; fileProfile = null; mipContext = null;使用下列值取代原始碼中的佔位元值:
佔位符 價值 <輸入檔案路徑> 測試輸入檔的完整路徑,例如: c:\\Test\\Test.docx。<標籤識別碼> 例如:從上一個快速入門的控制台輸出中複製的敏感度標籤識別碼: f42a3342-8706-4288-bd31-ebb85995028z。<輸出檔案路徑> 輸出檔案的完整路徑,這會是輸入檔的標籤複本,例如: c:\\Test\\Test_labeled.docx。
建置及測試應用程式
建置及測試客戶端應用程式。
使用 CTRL-SHIFT-B (建置解決方案) 來建置用戶端應用程式。 如果您沒有建置錯誤,請使用 F5 (開始偵錯) 來執行應用程式。
如果您的專案建置並成功執行,應用程式 可能會在 每次 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 租用戶的帳戶登入(且已設定敏感度標籤),系統可能會提示您登入,才能開啟標籤的文件。