文件 SDK 重新发布快速入门 (C++)

概述

有关此方案及其使用位置的概述,请参阅在 MIP SDK 中重新发布

先决条件

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

向 FileHandler Observer 类添加逻辑

为了能够通过 mip::FileHandler 公开的 GetDecryptedTemporaryFileAsync() 方法使用解密受保护的文件,成功和失败的异步方法的回调必须定义如下。

  1. 打开在前面的“快速入门:设置/获取敏感度标签 (C++)”中创建的 Visual Studio 解决方案。

  2. 使用解决方案资源管理器,在项目中打开 filehandler_observer.h 文件。 在 FileHandler 定义的末尾,在 }; 之前添加以下行用于方法声明。

        void OnGetDecryptedTemporaryFileSuccess(const std::string& decryptedFilePath, const std::shared_ptr<void>& context) override;
        void OnGetDecryptedTemporaryFileFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;
    
  3. 使用解决方案资源管理器,在项目中打开 filehandler_observer.cpp 文件。 在文件末尾,添加以下行用于方法声明。

    
        void FileHandlerObserver::OnGetDecryptedTemporaryFileSuccess(const std::string& decryptedFilePath, const std::shared_ptr<void>& context) {
        auto promise = std::static_pointer_cast<std::promise<std::string>>(context);
        promise->set_value(decryptedFilePath);
        }
    
        void FileHandlerObserver::OnGetDecryptedTemporaryFileFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) {
        auto promise = std::static_pointer_cast<std::promise<std::string>>(context);
        promise->set_exception(error);
        }
    

添加逻辑以编辑和重新发布受保护的文件

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

  2. 在 main() 主体的末尾,在 system("pause"); 之下和 return 0; 之上(你在上一个快速入门中离开的位置),插入以下代码:

//Originally protected file's path.
std::string protectedFilePath = "<protected-file-path>";

// Create file handler for the file
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
auto handlerFuture = handlerPromise->get_future();
engine->CreateFileHandlerAsync(protectedFilePath, 
                                protectedFilePath, 
                                true, 
                                std::make_shared<FileHandlerObserver>(), 
                                handlerPromise);
auto protectedFileHandler = handlerFuture.get();

// retieve and store protection handler from file
auto protectionHandler = protectedFileHandler->GetProtection();

//Check if the user has the 'Edit' right to the file and if so decrypt the file.
if (protectionHandler->AccessCheck("Edit")) {

    // Decrypt file to temp path using the same file handler
    auto tempPromise = std::make_shared<std::promise<string>>();
    auto tempFuture = tempPromise->get_future();
    protectedFileHandler->GetDecryptedTemporaryFileAsync(tempPromise);
    auto tempPath = tempFuture.get();

    /// Write code here to perform further operations for edit ///

    /// Follow steps below for re-protecting the edited file ///

    // Create a new file handler using the temporary file path.
    auto reprotectPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
    auto reprotectFuture = reprotectPromise->get_future();
    engine->CreateFileHandlerAsync(tempPath, 
                                    tempPath, 
                                    true, 
                                    std::make_shared<FileHandlerObserver>(), 
                                    reprotectPromise);
    auto republishHandler = reprotectFuture.get();

    // Set protection using the ProtectionHandler from the original consumption operation.
    republishHandler->SetProtection(protectionHandler);
    std::string reprotectedFilePath = "<protected-file-path>";

    // Commit changes
    auto republishPromise = std::make_shared<std::promise<bool>>();
    auto republishFuture = republishPromise->get_future();
    republishHandler->CommitAsync(reprotectedFilePath, republishPromise);

    // Validate republishing
    cout << "Protected File: " + protectedFilePath<<endl;
    cout << "Protected Label ID: " + protectedFileHandler->GetLabel()->GetLabel()->GetId() << endl;
    cout << "Protection Owner: " + protectedFileHandler->GetProtection()->GetOwner() << endl<<endl;

    cout << "Republished File: " + reprotectedFilePath<<endl;
    cout << "Republished Label ID: " + republishHandler->GetLabel()->GetLabel()->GetId() << endl;
    cout << "Republished Owner: " + republishHandler->GetProtection()->GetOwner() << endl;
}
  1. 在 Main() 的末尾,找到在上一个快速入门中创建的应用程序关闭块,并添加下面的处理程序行以释放资源。

        protectedFileHandler = nullptr;
        protectionHandler = nullptr;
    
    
  2. 将源代码中的占位符值替换为以下值:

    占位符
    <protected-file-path> 上一快速入门中的受保护文件。
    <reprotected-file-path> 要重新发布的已修改文件的输出文件路径。

生成并测试应用

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

  1. 使用 CTRL-SHIFT-B(生成解决方案)生成客户端应用程序。 如果没有生成错误,请使用 F5(启动调试)运行应用程序。

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

  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 . . .

  Run the PowerShell script to generate an access token using the following values, then copy/paste it below:
  Set $authority to: https://login.windows.net/37f4583d-9985-4e7f-a1ab-71afd8b55ba0
  Set $resourceUrl to: https://aadrm.com
  Sign in with user account: user1@tenant.onmicrosoft.com
  Enter access token: <paste-access-token-here>
  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 . . .
  Protected File: C:\Test\Test_labeled.docx
  Protected Label ID: 074e457c-5848-4542-9a6f-34a182080e7z
  Protection Owner: user1@tenant.onmicrosoft.com

  Republished File: c:\Test\Test_republished.docx
  Republished Label ID: 074e457c-5848-4542-9a6f-34a182080e7z
  Republished Owner: user1@tenant.onmicrosoft.com

  Press any key to close this window . . .