다음을 통해 공유


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