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

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

先决条件

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

实现观察程序类来监视文件处理程序对象

就像你在“应用程序初始化”快速入门中为文件配置对象和引擎实现观察器那样,现在请为文件处理器对象实现一个观察器类。

通过扩展 SDK 的 mip::FileHandler::Observer 类为文件处理程序观察程序创建基本实现。 应用程序实例化观察程序,稍后使用它来监视文件处理程序操作。

  1. 打开在前文“快速入门:列出敏感度标签 (C++)”中使用的 Visual Studio 解决方案。

  2. 向项目添加新类。 Visual Studio会为你生成 header/.h 和 implementation/.cpp 文件:

    • 在“解决方案资源管理器”中,再次右键单击项目节点,选择“添加”,然后选择“类”。
    • 在“添加类”对话框中:
      • 在“类名”字段中,输入“filehandler_observer”。 请注意,系统会根据输入的名称自动填充“.h 文件”和“.cpp 文件”字段。
      • 完成后,选择“确定”。
  3. Visual Studio为类生成 .h 和.cpp文件后,这两个文件都在编辑器选项卡中打开。 更新每个文件,以实现新的观察者类:

    • 通过选择/删除生成的 filehandler_observer 类来更新“filehandler_observer.h”。 不要删除上一步生成的预处理器指令(#pragma、#include)。 然后,将以下源复制/粘贴到文件中的任何现有预处理器指令之后:

      #include <memory>
      #include "mip/file/file_engine.h"
      #include "mip/file/file_handler.h"
      
      class FileHandlerObserver final : public mip::FileHandler::Observer {
      public:
         FileHandlerObserver() { }
         // Observer implementation
         void OnCreateFileHandlerSuccess(const std::shared_ptr<mip::FileHandler>& fileHandler, const std::shared_ptr<void>& context) override;
         void OnCreateFileHandlerFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;
         void OnCommitSuccess(bool committed, const std::shared_ptr<void>& context) override;
         void OnCommitFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;		
      };
      
    • 通过选择/删除生成的 filehandler_observer 类实现来更新“filehandler_observer.cpp”。 不要删除上一步生成的预处理器指令(#pragma、#include)。 然后,将以下源复制/粘贴到文件中的任何现有预处理器指令之后:

      void FileHandlerObserver::OnCreateFileHandlerSuccess(const std::shared_ptr<mip::FileHandler>& fileHandler, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
         promise->set_value(fileHandler);
      }
      
      void FileHandlerObserver::OnCreateFileHandlerFailure(const std::exception_ptr & error, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
         promise->set_exception(error);
      }
      
      void FileHandlerObserver::OnCommitSuccess(bool committed, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<bool>>(context);
         promise->set_value(committed);
      }
      
      void FileHandlerObserver::OnCommitFailure(const std::exception_ptr & error, const std::shared_ptr<void>& context) {
         auto promise = std::static_pointer_cast<std::promise<bool>>(context);
         promise->set_exception(error);
      }
      
  4. (可选)使用 F6(生成解决方案)运行解决方案的测试编译/链接,并在继续之前确认它已成功生成。

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

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

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

  2. 在文件顶部的相应现有指令后面添加以下内容 #include 和 using 指令:

    #include "filehandler_observer.h" 
    #include "mip/file/file_handler.h" 
    
    using mip::FileHandler;
    
  3. 在 main() body 末尾附近、上一个快速入门中你结束的位置,即位于 system("pause"); 之后且在 return 0; 之前,插入以下代码:

    // Set up async FileHandler for input file operations
    string inputFilePath = "<input-file-path>";
    string actualFilePath = "<content-identifier>";
    std::shared_ptr<FileHandler> handler;
    try
    {
         auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
         auto handlerFuture = handlerPromise->get_future();
         engine->CreateFileHandlerAsync(
              inputFilePath,
              actualFilePath,                       
              true, 
              std::make_shared<FileHandlerObserver>(), 
              handlerPromise);
         handler = handlerFuture.get();
    }
    catch (const std::exception& e)
    {
         cout << "An exception occurred... did you specify a valid input file path?\n\n" << e.what() << "'\n";
         system("pause");
         return 1;
    }
    
    // Set a label on input file
    try
    {
         string labelId = "<label-id>";
         cout << "\nApplying Label ID " << labelId << " to " << inputFilePath << endl;
         mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
         handler->SetLabel(engine->GetLabelById(labelId), labelingOptions, mip::ProtectionSettings());
    }
    catch (const std::exception& e)
    {
         cout << "An exception occurred... did you specify a valid label ID?\n\n" << e.what() << "'\n";
         system("pause");
         return 1; 
    }
    
    // Commit changes, save as a different/output file
    string filePathOut = "<output-file-path>";
    try
    {
     	cout << "Committing changes" << endl;
         auto commitPromise = std::make_shared<std::promise<bool>>();
         auto commitFuture = commitPromise->get_future();
         handler->CommitAsync(filePathOut, commitPromise);
     	if (commitFuture.get()) {
     		cout << "\nLabel committed to file: " << filePathOut << endl;
     	}
     	else {
     		cout << "Failed to label: " + filePathOut << endl;
     		return 1;
     	}
    }
    catch (const std::exception& e)
    {
         cout << "An exception occurred... did you specify a valid commit file path?\n\n" << e.what() << "'\n";
         system("pause");
         return 1;
    }
    system("pause");
    
    // Set up async FileHandler for output file operations
    actualFilePath = "<content-identifier>";
    try
    {
         auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
         auto handlerFuture = handlerPromise->get_future();
         engine->CreateFileHandlerAsync(
              filePathOut,
              actualFilePath,
              true,
              std::make_shared<FileHandlerObserver>(),
              handlerPromise);
    
         handler = handlerFuture.get();
    }
    catch (const std::exception& e)
    {
         cout << "An exception occurred... did you specify a valid output file path?\n\n" << e.what() << "'\n";
         system("pause");
         return 1;
    }
    
    // Get the label from output file
    try
    {
         cout << "\nGetting the label committed to file: " << filePathOut << endl;
         auto label = handler->GetLabel();
         cout << "Name: " + label->GetLabel()->GetName() << endl;
         cout << "Id: " + label->GetLabel()->GetId() << endl;
    }
    catch (const std::exception& e)
    {
         cout << "An exception occurred... did you specify a valid label ID?\n\n" << e.what() << "'\n";
         system("pause");
         return 1;
    }
    system("pause");
    
  4. 在 main() 的末尾附近,找到在第一个快速入门中创建的应用程序关闭代码块,并取消对处理程序所在代码行的注释:

    // Application shutdown. Null out profile and engine, call ReleaseAllResources();
    // Application may crash at shutdown if resources aren't properly released.
    profile = nullptr;
    engine = nullptr;
    handler = nullptr;
    mipContext = nullptr;
    
  5. 使用字符串常量替换源代码中的占位符值,如下所示:

    占位符 价值
    <输入文件路径> 测试输入文件的完整路径,例如:"c:\\Test\\Test.docx"。
    <内容标识符> 内容的易记标识符。 例如:
    • 对于文件,请使用 path\filename: "c:\Test\Test.docx"
    • 对于电子邮件,请使用 subject:sender: "RE: Audit design:user1@contoso.com"
    <标签-ID> 从上一快速入门中的控制台输出复制的敏感度标签 ID,例如:"f42a3342-8706-4288-bd31-ebb85995028z"。
    <输出文件路径> 输出文件的完整路径,这是输入文件的标记副本,例如: "c:\\Test\\Test_labeled.docx"

生成并测试应用

生成和测试客户端应用程序。

  1. 使用 F6(生成解决方案)生成客户端应用程序。 如果生成没有错误,请使用 F5(开始调试)来运行应用程序。

  2. 如果项目生成并成功运行,则每次 SDK 调用 AcquireOAuth2Token() 方法时,应用程序都会提示输入访问令牌。 正如你之前在“列出敏感度标签”快速入门中所做的那样,请运行 PowerShell 脚本,每次都使用为 $authority 和 $resourceUrl 提供的值来获取令牌。

    Run the PowerShell script to generate an access token using the following values, then copy/paste it below:
    
    Sensitivity labels for your organization:
    Non-Business : 87ba5c36-17cf-14793-bbc2-bd5b3a9f95cz
    Public : 83867195-f2b8-2ac2-b0b6-6bb73cb33afz
    General : f42a3342-8706-4288-bd31-ebb85995028z
    Confidential : 074e457c-5848-4542-9a6f-34a182080e7z
    Highly Confidential : f55c2dea-db0f-47cd-8520-a52e1590fb6z
    Press any 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
    Name: Confidential
    Id: 074e457c-5848-4542-9a6f-34a182080e7z
    Press any key to continue . . .
    

通过打开输出文件并直观检查文档的信息保护设置来验证标签应用程序。

注释

如果你正在为 Office 文档添加标签,但未使用颁发该访问令牌的 Microsoft Entra 租户中的帐户登录(且已配置敏感度标签),系统可能会提示你先登录,然后才能打开已加标签的文档。

后续步骤