快速入門:清單保護範本(C++)

這個快速入門工具說明如何使用 MIP Protection SDK 列出使用者可用的保護範本。

先決條件

繼續前請先完成以下先修條件:

新增邏輯以列出保護範本

使用保護引擎物件加入列出使用者可用保護範本的邏輯。

  1. 開啟您在先前的「快速入門 - 用戶端應用程式初始化 - 保護 SDK (C++)》 文章中建立的 Visual Studio 解決方案。

  2. 方案總管 中,打開專案中包含該方法實作main()的 .cpp 檔案。 預設名稱為包含它的專案的同一名稱,而這是在您建立專案時指定的。

  3. using之後,在文件接近開頭處新增下列using mip::ProtectionEngine;指令:

    using std::endl;
    
  4. main() 主體接近結尾處、最後一個 catch 區塊的結尾右大括號 } 下方,以及 return 0; 上方(也就是你在上一個快速入門中停下來的地方),插入以下程式碼:

     // List protection templates
     const shared_ptr<ProtectionEngineObserver> engineObserver = std::make_shared<ProtectionEngineObserver>();
     // Create a context to pass to 'ProtectionEngine::GetTemplateListAsync'. That context will be forwarded to the
     // corresponding ProtectionEngine::Observer methods. In this case, we use promises/futures as a simple way to detect
     // the async operation completes synchronously.
     auto loadPromise = std::make_shared<std::promise<vector<shared_ptr<mip::TemplateDescriptor>>>>();
     std::future<vector<shared_ptr<mip::TemplateDescriptor>>> loadFuture = loadPromise->get_future();
     engine->GetTemplatesAsync(engineObserver, loadPromise);
     auto templates = loadFuture.get();
    
     cout << "*** Template List: " << endl;
    
     for (const auto& protectionTemplate : templates) {
         cout << "Name: " << protectionTemplate->GetName() << " : " << protectionTemplate->GetId() << endl;
     }
    
    

建立 PowerShell 腳本以產生存取令牌

請使用下列 PowerShell 指令碼,產生 SDK 在您的 AuthDelegateImpl::AcquireOAuth2Token 實作中所要求的存取權杖。 此指令碼會使用你先前在「MIP SDK 設定與組態」中安裝的 MSAL.PS 模組中的 Get-MsalToken cmdlet。

  1. 建立一個 PowerShell 腳本檔案(.ps1 副檔名),然後複製貼上以下腳本到檔案中:

    • $authority$resourceUrl 稍後會在下一節中更新。
    • $appId$redirectUri 更新為與你在 Microsoft Entra 應用程式註冊中指定的值相符。
    $authority = '<authority-url>'                   # Specified when SDK calls AcquireOAuth2Token()
    $resourceUrl = '<resource-url>'                  # Specified when SDK calls AcquireOAuth2Token()
    $appId = '<app-ID>'                              # App ID of the Microsoft Entra app registration
    $redirectUri = '<redirect-uri>'                  # Redirect URI of the Microsoft Entra app registration
    $scope = $resourceUrl + "/.default"
    $response = Get-MsalToken -ClientId $appId -Authority $authority -RedirectUri $redirectUri -Scopes $scope -Interactive
    $response.AccessToken | clip                     # Copy the access token text to the clipboard
    
  2. 儲存文稿檔案,以便稍後在用戶端應用程式要求時執行。

建置及測試應用程式

建立並測試你的客戶應用程式。

  1. 使用 Ctrl+Shift+B(建置解決方案)來建立你的客戶端應用程式。 如果建置沒有錯誤,就用 F5(開始除錯)來執行你的應用程式。

  2. 如果你的專案成功建置並執行,應用程式每次 SDK 呼叫你的 AcquireOAuth2Token() 方法時都會提示存取權杖。 如果多次提示且請求的值相同,你可以重複使用先前產生的代幣:

  3. 若要產生提示的存取令牌,請返回您的 PowerShell 腳稿並:

    • 更新$authority$resourceUrl變數。 它們必須與步驟 2 中主控台輸出中指定的值相符。

    • 執行 PowerShell 指令碼。 該 Get-MsalToken cmdlet 會觸發一個 Microsoft Entra 認證提示,類似以下範例。 在第 2 步的主控台輸出中指定相同的帳號。 成功登入後,PowerShell 會將存取權杖放入剪貼簿。

      Visual Studio 取得令牌登入。

    • 你可能還需要同意讓應用程式在登入帳號下存取 MIP API。 當 Microsoft Entra 應用程式註冊未經預先同意時,可能會出現這種情況(請參照「MIP SDK 設定和組態」),或者您可能是使用來自不同租用戶的帳戶登入(而不是應用程式註冊所在的租用戶)。 選擇 「接受 」以記錄你的同意。

      Visual Studio 同意。

  4. 在你將第 2 步的存取權杖貼入提示後,你的主控台輸出應該會顯示保護範本,類似以下範例:

    *** Template List:
    Name: Confidential \ All Employees : a74f5027-f3e3-4c55-abcd-74c2ee41b607
    Name: Highly Confidential \ All Employees : bb7ed207-046a-4caf-9826-647cff56b990
    Name: Confidential : 174bc02a-6e22-4cf2-9309-cb3d47142b05
    Name: Contoso Employees Only : 667466bf-a01b-4b0a-8bbf-a79a3d96f720
    
    C:\MIP Sample Apps\ProtectionQS\Debug\ProtectionQS.exe (process 8252) exited with code 0.
    To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
    
    Press any key to continue . . .
    

    備註

    複製並儲存一個或多個保護範本的 ID(例如 f42a3342-8706-4288-bd31-ebb85995028z),以便在下一個快速入門中使用。

故障排除

執行C++應用程式時發生問題

總結 錯誤訊息 解決方法
不正確的存取令牌 發生例外狀況...存取令牌不正確/過期嗎?

API 呼叫失敗:profile_add_engine_async 失敗,原因:[class mip::PolicySyncException] 取得原則失敗,要求失敗,HTTP 狀態碼:401,x-ms-diagnostics: [2000001;reason="隨要求提交的 OAuth 權杖無法解析。";error_category="invalid_token"],correlationId:[aaaa0000-bb11-2222-33cc-444444dddddd]'

C:\VSProjects\MipDev\Quickstarts\AppInitialization\x64\Debug\AppInitialization.exe (process 29924) 以代碼 0 結束。

按任意鍵關閉此視窗 。 . }
如果您的專案建置成功,但您會看到類似左側的輸出,則方法中 AcquireOAuth2Token() 可能會有無效或過期的令牌。 返回 建立 PowerShell 腳本以產生存取令牌,並重新產生存取令牌 、再次更新 AcquireOAuth2Token() 和重建/重新測試。 您也可以使用 jwt.ms 單頁 Web 應用程式,檢查並驗證令牌及其宣告。

下一步

既然您已瞭解如何列出已驗證使用者可用的保護範本,請嘗試下一個快速入門: