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

本快速入門說明如何使用 MIP 保護 SDK 來保護使用者可用的範本。

必要條件

如果您尚未完成,請務必先完成下列必要條件,再繼續進行:

新增邏輯以列出保護範本

使用保護引擎物件,新增邏輯以列出使用者可用的保護範本。

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

  2. 使用 方案總管 ,在您的專案中開啟包含 方法實作的 main() .cpp 檔案。 它預設為與您在專案建立期間指定的專案相同名稱。

  3. 在 之後 using mip::ProtectionEngine; ,在 檔案頂端附近新增下列 using 指示詞:

    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 腳本來產生您的實作中 AuthDelegateImpl::AcquireOAuth2Token SDK 所要求的存取權杖。 腳本會 Get-ADALToken 使用您稍早在「MIP SDK 安裝和設定」中安裝之 ADAL.PS 模組中的 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 Azure AD app registration
    $redirectUri = '<redirect-uri>'                  # 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. 儲存腳本檔案,以便稍後在用戶端應用程式要求時執行。

建置及測試應用程式

最後,建置及測試用戶端應用程式。

  1. 使用 Ctrl+Shift+b ( 建置解決方案 )來建置用戶端應用程式。 如果您沒有建置錯誤,請使用 F5 ( 開始偵錯 ) 來執行應用程式。

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

  3. 若要產生提示的存取權杖,請返回您的 PowerShell 腳本並:

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

    • 執行 PowerShell 腳本。 Cmdlet Get-ADALToken 會觸發 Microsoft Entra 驗證提示,類似于下列範例。 在步驟 2 的主控台輸出中指定相同的帳戶。 成功登入之後,存取權杖會放在剪貼簿上。

      Visual Studio acquire token sign-in

    • 您可能也需要同意,以允許應用程式在登入帳戶下執行時存取 MIP API。 當 Microsoft Entra 應用程式註冊未預先同意時,就會發生這種情況(如「MIP SDK 設定和設定」中所述),或您以來自不同租使用者的帳戶登入(而不是註冊應用程式所在的帳戶)。 只要按一下 [ 接受 ] 即可記錄您的同意。

      Visual Studio consent

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

    注意

    複製並儲存一或多個保護範本的識別碼, f42a3342-8706-4288-bd31-ebb85995028z 因為您將在下一個快速入門中使用它。

疑難排解

C++ 應用程式執行期間的問題

摘要 錯誤訊息 解決方案
不正確的存取權杖 發生例外狀況...存取權杖不正確/過期嗎?

失敗的 API 呼叫:profile_add_engine_async失敗,發生:[類別 mip::P olicySyncException] 無法取得原則,要求失敗,HTTP 狀態碼為 401,x-ms-diagnostics: [2000001;reason=「與要求一起提交的 OAuth 權杖無法剖析。」;error_category=「invalid_token」], correlationId:[35bc0023-3727-4eff-8062-00006d672]'

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

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

後續步驟

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

[加密和解密文字](quick-protection-encrypt-decrypt text-cpp.md)