快速入门:设置和获取敏感度标签 (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. 使用以下值替换源代码中的占位符值:

    占位符 价值
    <输入文件路径> 测试输入文件的完整路径,例如: c:\\Test\\Test.docx
    <label-id> 从上一快速入门中的控制台输出复制的敏感度标签 ID,例如: f42a3342-8706-4288-bd31-ebb85995028z
    <输出文件路径> 输出文件的完整路径,该文件将是输入文件的标记副本,例如: 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 租户中的帐户登录(并且配置了敏感度标签),则系统可能会提示你登录,然后才能打开标记的文档。