Poznámka
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
This Quickstart shows you how to use more of the MIP File SDKs. Using one of the sensitivity labels you listed in the previous Quickstart, you use a File handler to set/get the label on a file. The File handler class exposes various operations for setting/getting labels, or protection, for supported file types.
Požadavky
Pokud jste to ještě neudělali, nezapomeňte před pokračováním dokončit následující požadavky:
- Complete Quickstart: List sensitivity labels (C++) first, which builds a starter Visual Studio solution, to list an organization's sensitivity labels. This "Set and get a sensitivity label" Quickstart builds on the previous one.
- Volitelně: Zkontrolujte obslužné rutiny souborů v konceptech sady MIP SDK .
Implement an observer class to monitor the File handler object
Similar to the observer you implemented (for the File profile and engine) in the Application initialization Quickstart, now you implement an observer class for a File handler object.
Create a basic implementation for a File handler observer, by extending the SDK's mip::FileHandler::Observer
class. The observer is instantiated and used later, to monitor File handler operations.
Open the Visual Studio solution you worked on in the previous "Quickstart: List sensitivity labels (C++)" article.
Přidejte do projektu novou třídu, která za vás vygeneruje soubory header/.h i implementace/.cpp:
- V Průzkumníku řešení znovu klikněte pravým tlačítkem myši na uzel projektu, vyberte Přidat a pak vyberte Třída.
- On the Add Class dialog:
- In the Class Name field, enter "filehandler_observer". Všimněte si, že se automaticky vyplní pole souboru .h i .cpp na základě zadaného názvu.
- Po dokončení klikněte na tlačítko OK .
Po vygenerování souborů .h a .cpp pro třídu se oba soubory otevřou na kartách Skupiny editoru. Teď aktualizujte každý soubor tak, aby implementovali novou třídu pozorovatele:
Update "filehandler_observer.h", by selecting/deleting the generated
filehandler_observer
class. Neodebíjejte direktivy preprocesoru generované předchozím krokem (#pragma, #include). Potom zkopírujte/vložte následující zdroj do souboru po všech existujících direktivách preprocesoru.#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; };
Update "filehandler_observer.cpp", by selecting/deleting the generated
filehandler_observer
class implementation. Neodebíjejte direktivy preprocesoru generované předchozím krokem (#pragma, #include). Potom zkopírujte/vložte následující zdroj do souboru po všech existujících direktivách preprocesoru.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); }
Volitelně můžete pomocí F6 (sestavit řešení) spustit testovací kompilaci nebo propojení vašeho řešení, abyste se ujistili, že se sestavení úspěšně sestaví, než budete pokračovat.
Add logic to set and get a sensitivity label
Add logic to set and get a sensitivity label on a file, using the File engine object.
Pomocí Průzkumníka řešení otevřete soubor .cpp v projektu, který obsahuje implementaci
main()
metody. V rámci výchozího nastavení se použije stejný název jako projekt, který ho obsahuje a který jste zadali při vytváření projektu.Add the following
#include
andusing
directives, below the corresponding existing directives, at the top of the file:#include "filehandler_observer.h" #include "mip/file/file_handler.h" using mip::FileHandler;
Toward the end of the
main()
body, belowsystem("pause");
and abovereturn 0;
(where you left off in the previous Quickstart), insert the following code:// 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");
Toward the end of
main()
find the application shutdown block created in the first quickstart and uncomment the handler line:// 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;
Replace the placeholder values in the source code that you as follows, using string constants:
Placeholder Hodnota <input-file-path> The full path to a test input file, for example: "c:\\Test\\Test.docx"
.<content-identifier> A human-readable identifier for the content. Například: - for a file, consider path\filename:
"c:\Test\Test.docx"
- for an email, consider subject:sender :
"RE: Audit design:user1@contoso.com"
<identifikátor štítku> A sensitivity label ID, copied from the console output in the previous Quickstart, for example: "f42a3342-8706-4288-bd31-ebb85995028z"
.<cesta k výstupnímu souboru> Úplná cesta k výstupnímu souboru, což bude označená kopie vstupního souboru, například: "c:\\Test\\Test_labeled.docx"
.- for a file, consider path\filename:
Sestavení a otestování aplikace
Sestavte a otestujte klientskou aplikaci.
K sestavení klientské aplikace použijte klávesu F6 (Build Solution). Pokud nemáte žádné chyby sestavení, spusťte aplikaci pomocí klávesy F5 (spustit ladění).
Pokud se projekt sestaví a úspěšně spustí, aplikace vyzve k zadání přístupového tokenu pokaždé, když sada SDK volá vaši
AcquireOAuth2Token()
metodu. As you did previously in the "List sensitivity labels" Quickstart, run your PowerShell script to acquire the token each time, using the values provided for $authority and $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 . . .
You can verify the application of the label, by opening the output file and visually inspecting the document's information protection settings.
Poznámka:
If you're labeling an Office document, but not signed in using an account from the Microsoft Entra tenant where the access token was obtained (and sensitivity labels are configured), you may be prompted to sign in before you can open the labelled document.