快速入门:设置和获取敏感度标签 (C#)

本快速入门介绍如何使用更多 MIP 文件 SDK。 通过上一个快速入门中列出的其中一个敏感度标签,可以使用文件处理程序来设置/获取文件上的标签。 文件处理程序类公开用于为支持的文件类型设置/获取标签或保护的各种操作。

先决条件

如果尚未完成,请确保先完成以下先决条件,然后再继续:

添加逻辑以设置和获取敏感度标签

使用文件引擎对象添加逻辑以设置和获取文件的敏感度标签。

  1. 使用解决方案资源管理器,打开项目中包含 Main()` 方法实现的 .cs 文件。 该文件默认与包含它的项目同名,该名称在项目创建期间指定。

  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> 从上一快速入门中的控制台输出复制的敏感度标签 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 租户中的帐户登录,系统可能会提示先进行登录,然后才能打开标记的文档。