Mulai cepat: Mengatur dan mendapatkan label sensitivitas (C++)

Panduan mulai cepat ini menunjukkan cara menggunakan lebih banyak MIP File SDK. Dengan menggunakan salah satu label sensitivitas yang Anda cantumkan di panduan mulai cepat sebelumnya, Anda menggunakan handler file untuk mengatur dan mengambil label pada file. Kelas handler file mengekspos berbagai operasi untuk mengatur dan mendapatkan label, atau perlindungan, untuk jenis file yang didukung.

Prasyarat

Jika Anda belum melakukannya, pastikan untuk menyelesaikan prasyarat berikut sebelum melanjutkan:

Menerapkan kelas pengamat untuk memantau objek handler File

Seperti pada observer yang Anda implementasikan untuk profil berkas dan engine dalam panduan singkat inisialisasi aplikasi, kini Anda mengimplementasikan kelas observer untuk objek file handler.

Buat implementasi dasar untuk pengamat handler file dengan memperluas kelas SDK mip::FileHandler::Observer . Aplikasi membuat instans pengamat dan menggunakannya nanti untuk memantau operasi penanganan file.

  1. Buka solusi Visual Studio yang Anda kerjakan di artikel "Mulai Cepat: Daftar label sensitivitas (C++)" sebelumnya.

  2. Tambahkan kelas baru ke proyek Anda. Visual Studio menghasilkan file header/.h dan implementasi/.cpp untuk Anda:

    • Di Penjelajah Solusi, klik kanan simpul proyek lagi, pilih Tambahkan, lalu pilih Kelas.
    • Pada dialog Tambahkan Kelas :
      • Di bidang Nama Kelas , masukkan "filehandler_observer". Perhatikan bahwa file .h dan bidang file .cpp diisi secara otomatis, berdasarkan nama yang Anda masukkan.
      • Setelah selesai, pilih OK.
  3. Setelah Visual Studio menghasilkan file .h dan .cpp untuk kelas, kedua file terbuka di tab editor. Perbarui setiap file untuk mengimplementasikan kelas pengamat baru Anda:

    • Perbarui "filehandler_observer.h", dengan memilih/menghapus kelas yang dihasilkan filehandler_observer . Jangan hapus direktif pra-prosesor yang dihasilkan oleh langkah sebelumnya (#pragma, #include). Kemudian salin/tempel sumber berikut ke dalam file, setelah direktif praprosesor yang ada:

      #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;		
      };
      
    • Perbarui "filehandler_observer.cpp", dengan memilih/menghapus implementasi kelas yang dihasilkan filehandler_observer . Jangan hapus direktif pra-prosesor yang dihasilkan oleh langkah sebelumnya (#pragma, #include). Kemudian salin/tempel sumber berikut ke dalam file, setelah direktif praprosesor yang ada:

      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. Secara opsional, gunakan F6 (Build Solution) untuk menjalankan kompilasi/tautan pengujian solusi Anda dan konfirmasikan bahwa solusi berhasil dibuat sebelum melanjutkan.

Tambahkan logika untuk menetapkan dan mendapatkan label sensitivitas

Gunakan objek mesin File untuk menambahkan logika yang menetapkan dan mengambil label sensitivitas pada file.

  1. Di Penjelajah Solusi, buka file .cpp dalam proyek Anda yang berisi implementasi main() metode . Ini menggunakan nama default yang sama dengan proyek yang memuatnya, yang Anda tentukan selama pembuatan proyek.

  2. Tambahkan arahan berikut: using dan #include setelah arahan yang sudah ada dan sesuai di bagian atas file:

    #include "filehandler_observer.h" 
    #include "mip/file/file_handler.h" 
    
    using mip::FileHandler;
    
  3. Menjelang akhir isi main(), setelah system("pause"); dan sebelum return 0; tempat Anda berhenti pada panduan memulai cepat sebelumnya, sisipkan kode berikut:

    // 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 " << filePathIn << endl;
         mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
         handler->SetLabel(engine->GetLabelById(labelId), labelingOptions, new 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. Menjelang bagian akhir main(), temukan blok penghentian aplikasi yang dibuat dalam panduan memulai cepat pertama dan hapus tanda komentar pada baris penangan:

    // 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. Ganti nilai placeholder dalam kode sumber sebagai berikut dengan menggunakan konstanta string:

    Pengganti sementara Nilai
    <input-file-path> Jalur lengkap ke file masukan uji, misalnya: "c:\\Test\\Test.docx".
    <pengidentifikasi konten> Pengidentifikasi konten yang mudah dibaca oleh manusia. Misalnya:
    • Untuk file, gunakan path\filename: "c:\Test\Test.docx"
    • Untuk email, gunakan subject:sender: "RE: Audit design:user1@contoso.com"
    <label-id> ID label sensitivitas, disalin dari output konsol di Quickstart sebelumnya, contohnya: "f42a3342-8706-4288-bd31-ebb85995028z".
    <output-file-path> Jalur lengkap ke file output, yang merupakan salinan berlabel file input, misalnya: "c:\\Test\\Test_labeled.docx".

Membangun dan menguji aplikasi

Buat dan uji aplikasi klien Anda.

  1. Gunakan F6 (Build Solution) untuk membangun aplikasi klien Anda. Jika build tidak memiliki kesalahan, gunakan F5 (Mulai penelusuran kesalahan) untuk menjalankan aplikasi Anda.

  2. Jika proyek Anda berhasil membangun dan berjalan, aplikasi meminta token akses setiap kali SDK memanggil metode Anda AcquireOAuth2Token() . Seperti yang Anda lakukan sebelumnya dalam panduan mulai cepat "Daftar label sensitivitas", jalankan skrip PowerShell Anda untuk mendapatkan token setiap kali menggunakan nilai yang disediakan untuk $authority dan $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 . . .
    

Verifikasi aplikasi label dengan membuka file output dan memeriksa pengaturan perlindungan informasi dokumen secara visual.

Nota

Jika Anda memberi label pada dokumen Office tetapi belum masuk menggunakan akun dari tenant Microsoft Entra yang menerbitkan token akses (dan label sensitivitas dikonfigurasi), Anda mungkin diminta untuk masuk sebelum dapat membuka dokumen yang telah diberi label.

Langkah berikutnya