次の方法で共有


File SDK の再発行に関するクイック スタート (C++)

概要

このシナリオの概要と使用できる場所については、 MIP SDK での再発行に関するページを参照してください。

前提条件

まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。

  • 完全な クイック スタート: 秘密度ラベルを設定/取得する (C++) 最初に、組織の秘密度ラベルを一覧表示するスターター Visual Studio ソリューションを構築し、ファイルとの間で秘密度ラベルを設定および読み取ります。 この「方法 - 理由 C++ が必要なラベルをダウングレードまたは削除する」クイック スタートは、前のクイック スタートに基づいています。
  • 必要に応じて、MIP SDK の概念で ファイル ハンドラー を確認します。
  • 必要に応じて、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. 次の値を使用して、ソース コード内のプレースホルダーの値を置き換えます。

    プレースホルダー 価値
    <保護されたファイルパス> 前のクイック スタートから保護されたファイル。
    <再保護されたファイルパス> 再発行する変更されたファイルの出力ファイル パス。

アプリケーションのビルドとテスト

クライアント アプリケーションをビルドしてテストします。

  1. Ctrl -SHIFT-B (ソリューションのビルド) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、F5 (デバッグの開始) を使用してアプリケーションを実行します。

  2. プロジェクトが正常にビルドされて実行されると、SDK が AcquireOAuth2Token() メソッドを呼び出すたびに、アプリケーションによってアクセス トークンの入力が求められます。 前の「秘密度ラベルの設定/取得」クイック スタートで行ったように、PowerShell スクリプトを実行して毎回、$authorityと$resourceUrlに指定された値を使用してトークンを取得します。

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