파일 SDK 재게시 빠른 시작 가이드(C++)

개요

이 시나리오의 개요와 이 시나리오를 사용할 수 있는 위치는 MIP SDK에서 다시 게시를 참조하세요.

필수 조건

아직 완료하지 않은 경우 계속하기 전에 다음 필수 조건을 완료해야 합니다.

  • 빠른 시작: 민감도 레이블 설정 및 가져오기(C++). 먼저 이 빠른 시작을 완료합니다. 조직의 민감도 레이블을 나열하고 파일에서 민감도 레이블을 설정하고 읽는 스타터 Visual Studio 솔루션을 빌드합니다. 이 "방법: 근거가 필요한 레이블을 다운그레이드/제거하는 방법(C++)" 빠른 시작 가이드는 이전 가이드를 바탕으로 합니다.
  • 선택 사항: MIP SDK 개념에서 파일 처리기를 검토합니다.
  • 선택 사항: MIP SDK 개념에서 보호 처리기를 검토합니다.

FileHandler 관찰자 클래스에 논리 추가

보호된 파일을 해독하려면 GetDecryptedTemporaryFileAsync()에서 제공하는 mip::FileHandler 메서드를 호출합니다. 다음 예제와 같이 비동기 메서드에 대한 성공 및 실패 콜백을 정의합니다.

  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. 본문의 끝부분에서, 이전 빠른 시작에서 중단한 지점인 system("pause"); 뒤와 return 0; 앞 사이의 main() 위치에 다음 코드를 삽입하세요.

//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() . 이전에 "민감도 레이블 설정 및 가져오기" 빠른 시작에서 했던 것처럼, $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.microsoftonline.com/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 . . .

다음 단계