這個快速入門指南會教你如何使用更多 MIP 檔案 SDK。 透過使用你在前述快速入門中列出的敏感度標籤之一,你可以使用檔案處理程式來設定並取得檔案上的標籤。 檔案處理類別會提供各種操作,用於設定及取得支援檔案類型的標籤或保護。
先決條件
如果您尚未完成,請務必先完成下列必要條件,再繼續進行:
- 快速入門:列出敏感度標籤(C#)。 先完成這個快速入門。 它會建立一個入門的 Visual Studio 解決方案,列出組織的敏感性標籤。 這個「設定並取得敏感度標籤」快速入門是建立在前一個基礎上的。
- 選擇性:檢閱 MIP SDK 概念中的檔案處理程式 。
新增邏輯以設定並取得敏感度標籤
使用 File Engine 物件來新增邏輯,設定並取得檔案的敏感度標籤。
在方案總管中,打開專案中包含該方法實作
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()方法時(通常是透過 MSAL.NET)提示驗證。 如果快取的認證已經存在,您將不會被提示登入,也不會看到卷標清單以及已套用標籤和已修改檔案的資訊。
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 租戶帳號登入(且敏感度標籤已設定),你可能會被要求先登入才能開啟標籤文件。