Udostępnij przez


Quickstart: List sensitivity labels (C++)

This Quickstart shows you how to use the MIP File SDK, to list the sensitivity labels configured for your organization.

Wymagania wstępne

Jeśli jeszcze tego nie zrobiono, przed kontynuowaniem upewnij się, że zostały spełnione następujące wymagania wstępne:

Add logic to list the sensitivity labels

Add logic to list your organization's sensitivity labels, using the File engine object.

  1. Otwórz rozwiązanie programu Visual Studio utworzone w poprzednim artykule "Szybki start: inicjowanie aplikacji klienckiej (C++)".

  2. Za pomocą Eksploratora rozwiązań otwórz plik .cpp w projekcie zawierający implementację main() metody . Domyślnie ma taką samą nazwę jak projekt zawierający go, który został określony podczas tworzenia projektu.

  3. Add the following using directive after using mip::FileEngine;, near the top of the file:

    using std::endl;
    
  4. Toward the end of the main() body, below the closing brace } of the last catch block and above return 0; (where you left off in the previous Quickstart), insert the following code:

    // List sensitivity labels
    cout << "\nSensitivity labels for your organization:\n";
    auto labels = engine->ListSensitivityLabels();
    for (const auto& label : labels)
    {
       cout << label->GetName() << " : " << label->GetId() << endl;
    
       for (const auto& child : label->GetChildren())
       {
         cout << "->  " << child->GetName() << " : " << child->GetId() << endl;
       }
    }
    system("pause");
    

Create a PowerShell script to generate access tokens

Use the following PowerShell script to generate access tokens, which are requested by the SDK in your AuthDelegateImpl::AcquireOAuth2Token implementation. The script uses the Get-ADALToken cmdlet from the ADAL.PS module you installed earlier, in "MIP SDK Setup and configuration".

  1. Create a PowerShell Script file (.ps1 extension), and copy/paste the following script into the file:

    • $authority and $resourceUrl are updated later, in the following section.
    • Update $appId and $redirectUri, to match the values you specified in your Microsoft Entra app registration.
    $authority = '<authority-url>'                   # Specified when SDK calls AcquireOAuth2Token()
    $resourceUrl = '<resource-url>'                  # Specified when SDK calls AcquireOAuth2Token()
    $appId = '0edbblll-8773-44de-b87c-b8c6276d41eb'  # App ID of the Azure AD app registration
    $redirectUri = 'bltest://authorize'              # Redirect URI of the Azure AD app registration
    $response = Get-ADALToken -Resource $resourceUrl -ClientId $appId -RedirectUri $redirectUri -Authority $authority -PromptBehavior:RefreshSession
    $response.AccessToken | clip                     # Copy the access token text to the clipboard
    
  2. Save the script file so you can run it later, when requested by your client application.

Kompilowanie i testowanie aplikacji

Na koniec skompiluj i przetestuj aplikację kliencą.

  1. Use F6 (Build Solution) to build your client application. Jeśli nie masz błędów kompilacji, użyj F5 (Rozpocznij debugowanie), aby uruchomić aplikację.

  2. If your project builds and runs successfully, the application prompts for an access token, each time the SDK calls your AcquireOAuth2Token() method. You can reuse a previously generated token, if prompted multiple times and the requested values are the same.

  3. To generate an access token for the prompt, go back to your PowerShell script and:

    • Update the $authority and $resourceUrl variables. They must match the values that are specified in the console output in step #2. These values are provided by the MIP SDK in the challenge parameter of AcquireOAuth2Token():

    • Uruchom skrypt programu PowerShell. The Get-ADALToken cmdlet triggers a Microsoft Entra authentication prompt, similar to the example below. Specify the same account provided in the console output in step #2. After successful sign-in, the access token will be placed on the clipboard.

      Pozyskiwanie tokenu dla logowania w programie Visual Studio

    • Może być również konieczne wyrażenie zgody, aby zezwolić aplikacji na dostęp do interfejsów API usługi MIP podczas uruchamiania na koncie logowania. Dzieje się tak, gdy rejestracja aplikacji Microsoft Entra nie jest wcześniej zatwierdzona (zgodnie z opisem w sekcji "Konfiguracja zestawu MIP SDK") lub logujesz się przy użyciu konta z innego dzierżawcy (innego niż ten, w którym zarejestrowano aplikację). Po prostu kliknij przycisk Akceptuj , aby zarejestrować swoją zgodę.

      Zgoda programu Visual Studio

  4. After pasting the access token into the prompt from step #2, your console output should show the sensitivity labels, similar to the following example:

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

    Uwaga

    Copy and save the ID of one or more of the sensitivity labels (for example, f42a3342-8706-4288-bd31-ebb85995028z), as you will use it in the next Quickstart.

Rozwiązywanie problemów

Problems during execution of C++ application

Podsumowanie Komunikat o błędzie Rozwiązanie
Nieprawidłowy token dostępu Wystąpił wyjątek... czy token dostępu jest niepoprawny/wygasł?

Failed API call: profile_add_engine_async Failed with: [class mip::PolicySyncException] Failed acquiring policy, Request failed with http status code: 401, x-ms-diagnostics: [2000001;reason="OAuth token submitted with the request cannot be parsed.";error_category="invalid_token"], correlationId:[35bc0023-3727-4eff-8062-000006d5d672]'

C:\VSProjects\MipDev\Quickstarts\AppInitialization\x64\Debug\AppInitialization.exe (proces 29924) zakończył działanie z kodem 0.

Naciśnij dowolny klawisz, aby zamknąć to okno. . .
Jeśli projekt zostanie pomyślnie skompilowany, ale zobaczysz dane wyjściowe podobne do przedstawionych po lewej, prawdopodobnie masz nieprawidłowy lub wygasły token w funkcji AcquireOAuth2Token(). Go back to Create a PowerShell script to generate access tokens and regenerate the access token, update AcquireOAuth2Token() again, and rebuild/retest. Możesz również sprawdzić i zweryfikować token i jego oświadczenia przy użyciu aplikacji internetowej jwt.ms jednostronicowej.
Sensitivity labels aren't configured N/a If your project builds successfully, but you have no output in the console window, be sure your organization's sensitivity labels are configured correctly. Aby uzyskać szczegółowe informacje, zobacz Konfigurowanie i konfigurowanie zestawu MIP SDK w obszarze "Definiowanie taksonomii etykiet i ustawień ochrony".

Następne kroki

Now that you've learned how to list the sensitivity labels for your organization, try the next quickstart: