共用方式為


利用系統指定的管理身份認證 Azure 託管的 C++ 應用程式至 Azure 資源

向其他 Azure 資源驗證 Azure 裝載應用程式的建議方法是使用 受控識別。 此方法 支援大部分的 Azure 服務,包括裝載在 Azure App Service、Azure Container Apps 和 Azure 虛擬機上的應用程式。 在驗證概覽頁面上瞭解更多不同的驗證技術和方法。 在接下來的各節中,您將瞭解:

  • 基本受控識別概念
  • 如何為您的應用程式建立系統指派的受控識別
  • 如何將角色指派給系統指派的受管理身份
  • 如何從應用程式程式代碼使用系統指派的受控識別進行驗證

基本受控識別概念

受控識別可讓您的應用程式安全地連線到其他 Azure 資源,而不需要使用秘密金鑰或其他應用程式秘密。 在內部,Azure 會追蹤身分識別,以及允許其連線的資源。 Azure 會使用這項資訊自動取得應用程式Microsoft Entra 令牌,以允許它連線到其他 Azure 資源。

設定載入的應用程式時,需要考慮兩種類型的受控識別:

  • 系統指派 受控身分識別是在 Azure 資源上直接啟用的,並與其生命周期緊密相連。 刪除資源時,Azure 會自動為您刪除身分識別。 系統指派的身分識別提供使用受控識別的極簡方法。
  • 使用者指派的 管理的身分會建立為獨立的 Azure 資源,並提供更強的彈性與功能。 其適用於涉及多個需要共用相同身分識別和許可權之 Azure 資源的解決方案。 例如,如果多個虛擬機需要存取同一組 Azure 資源,則使用者指派的受控識別可提供重複使用性和優化管理。

小提示

如需如何深入瞭解如何選取和管理系統指派和使用者指派的受控識別,請參閱 受控識別最佳做法建議 一文。

下列各節說明為 Azure 裝載的應用程式啟用和使用系統指派受控識別的步驟。 如果您需要使用使用者指派的受控識別,請流覽 用戶指派的受控識別 文章以取得詳細資訊。

在 Azure 裝載資源上啟用系統指派的受控識別

若要開始使用系統指派的受控識別搭配您的應用程式,請在裝載應用程式的 Azure 資源上啟用身分識別,例如 Azure App Service、Azure Container App 或 Azure 虛擬機。

您可以使用 Azure 入口網站或 Azure CLI,為 Azure 資源啟用系統指派的受控識別。

  1. 在 Azure 入口網站中,瀏覽至裝載應用程式程式代碼的資源,例如 Azure App Service 或 Azure Container App 實例。

  2. 從資源的 [概觀] 頁面中,展開 [[設定],然後從導覽中選取 [身分識別]。

  3. 在 [身份] 頁面上,將 [狀態] 滑桿切換到 開啟

  4. 選取 [儲存] 以套用變更

    螢幕擷取畫面,示範如何在容器應用程式上啟用系統指派的受控識別。

將角色指派給管理的身分識別

接下來,判斷您的應用程式需要哪些角色,並將這些角色指派給受控識別。 您可以在下列範圍將角色指派給受控識別:

  • 資源:指派的角色僅適用於該特定資源。
  • 資源群組:指派的角色會套用至資源群組中包含的所有資源。
  • 訂用帳戶:指派的角色會套用至訂用帳戶中包含的所有資源。

下列範例示範如何在資源群組範圍指派角色,因為許多應用程式都會使用單一資源群組來管理其所有相關的 Azure 資源。

  1. 流覽至資源群組的 [概觀] 頁面,其中包含具有系統指派受控識別的應用程式。

  2. 請選取左側導覽列中的 存取控制 (IAM)

  3. 在 [存取控制 (IAM)] 頁面上,選取頂端功能表上的 [+ 新增],然後選擇 [新增角色指派] 導覽至 [新增角色指派] 頁面。

    螢幕擷取畫面,顯示如何存取身分識別角色指派頁面。

  4. [新增角色指派] 頁面會顯示多步驟的索引標籤式工作流程,以將角色指派給身分。 在 [初始 角色] 索引標籤上,使用頂端的搜尋方塊來找出您要指派給身分識別的角色。

  5. 從結果中選取角色,然後選擇 [下一步] 以移至 [成員] 標籤。

  6. 針對 [[指派存取權給] 選項,選取 [受控識別]。

  7. 針對 [成員] 選項,選取 [+ 選取成員] 以開啟 [選取受控識別] 面板。

  8. 在 [選取受控識別 面板上,使用 [訂用帳戶] 和 [受控 識別] 下拉式清單來篩選您身分識別的搜尋結果。 使用 [] 選取 [] 搜尋方塊,找出您為裝載應用程式的 Azure 資源啟用的系統身分識別。

    顯示受控識別指派程式的螢幕擷取畫面。

  9. 選取身分,然後在面板底部選擇[選取]以繼續。

  10. 請在頁面底部點選 ,然後選擇 [檢閱 + 指派]。

  11. 在最後一個 [檢閱 + 指派] 索引標籤上,選取 [檢閱 + 指派],以完成工作流程。

從您的應用程式向 Azure 服務進行驗證

Azure Identity 函式庫提供多種 憑證,這些實作是為了支援不同情境及 Microsoft Entra 驗證流程而調整。 由於在本機執行時無法使用受控身分識別,後續步驟會示範應該在什麼情況下使用哪種認證:

  • 本機開發環境只在本機開發時,請使用名為 DefaultAzureCredential 的類別,以便使用預先配置且有意見的認證鏈。 DefaultAzureCredential 會從本機工具或 IDE 探索使用者認證,例如 Azure CLI 或 Visual Studio。 它也提供重試的彈性和便利性、回應等候時間,以及支援多個驗證選項。 若要了解更多資訊,請瀏覽本機開發期間對 Azure 服務進行驗證的文章。
  • Azure 裝載的應用程式:當您的應用程式在 Azure 中執行時,請使用 ManagedIdentityCredential 安全地探索針對應用程式設定的受控識別。 指定此準確類型的認證可防止意外地選用其他可用的認證。

實作程序代碼

  1. 使用 vcpkgazure-identity-cpp 套件加入你的應用程式。

    在您選擇的終端機中,導覽至應用程式專案目錄,然後執行下列命令:

    vcpkg add port azure-identity-cpp
    
  2. 在你的 CMake 檔案中加入以下內容:

    find_package(azure-identity-cpp CONFIG REQUIRED)
    target_link_libraries(<your project name> PRIVATE Azure::azure-identity)
    
  3. Azure 服務是使用各種 Azure SDK 用戶端程式庫中的特製化用戶端來存取。 對於任何在應用程式中實例化 Azure SDK 用戶端的 C++ 程式碼,你需要:

    1. 請附上 azure/identity.hpp 標頭。
    2. 建立 DefaultAzureCredential 的執行個體。
    3. 將 的 DefaultAzureCredential 實例傳給 Azure SDK 客戶端建構器。
    4. 將環境變數設定 AZURE_TOKEN_CREDENTIALSManagedIdentityCredential 確保使用 DefaultAzureCredential 受管理身份憑證。 這種做法讓驗證在部署到 Azure 時更可預測且更容易除錯。 欲了解更多資訊,請參閱 使用特定憑證

    下列程式碼區段顯示其中一個使用 Azure Blob 儲存用戶端的步驟範例。

    #include <azure/identity.hpp>
    #include <azure/storage/blobs.hpp>
    #include <iostream>
    #include <memory>
    
    int main() {
        try {
            // Create a credential
            auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>(true);
    
            // Create a client for the specified storage account
            std::string accountUrl = "https://<replace_with_your_storage_account_name>.blob.core.windows.net/";
            Azure::Storage::Blobs::BlobServiceClient blobServiceClient(accountUrl, credential);
    
            // Get a reference to a container
            std::string containerName = "sample-container";
            auto containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    
            // Get a reference to a blob
            std::string blobName = "sample-blob";
            auto blobClient = containerClient.GetBlobClient(blobName);
    
            // TODO: perform some action with the blob client
            // auto downloadResult = blobClient.DownloadTo("path/to/local/file");
    
            std::cout << "Successfully authenticated and created Azure clients." << std::endl;
    
        } catch (const std::exception& ex) {
            std::cout << "Exception: " << ex.what() << std::endl;
            return 1;
        }
    
        return 0;
    }
    

Azure SDK for C++ 認證概述 文章所述,支援 DefaultAzureCredential 多種認證方法,並決定執行時所使用的驗證方法。 這種方法的好處是,您的應用程式可以在不同的環境中使用不同的驗證方法,而無需實作環境特定的程式碼。 在本機開發過程中,當您的工作站上執行上述程式碼時,DefaultAzureCredential會使用應用程式服務主體(依環境設定而定)或開發人員工具憑證來對其他 Azure 核心資源進行驗證。 因此,相同的程式碼可用來在本機開發期間和部署至 Azure 時向 Azure 資源驗證您的應用程式。

這很重要

DefaultAzureCredential 藉由結合 Azure 裝載環境中使用的認證和本機開發中使用的認證,在開發部署至 Azure 的應用程式時簡化驗證。 在生產環境中,最好使用特定的認證類型,讓驗證更可預測且更容易偵錯。

另一個替代方案 DefaultAzureCredential 是使用 ManagedIdentityCredential。 使用 ManagedIdentityCredential 步驟與使用 DefaultAzureCredential 類型相同。

下列程式碼區段顯示其中一個使用 Azure Blob 儲存用戶端的步驟範例。

#include <azure/identity.hpp>
#include <azure/storage/blobs.hpp>
#include <iostream>
#include <memory>

int main() {
    try {
        // Create a system-assigned managed identity credential
        auto credential = std::make_shared<Azure::Identity::ManagedIdentityCredential>();
        
        // Create a client for the specified storage account
        std::string accountUrl = "https://<replace_with_your_storage_account_name>.blob.core.windows.net/";
        Azure::Storage::Blobs::BlobServiceClient blobServiceClient(accountUrl, credential);
        
        // Get a reference to a container
        std::string containerName = "sample-container";
        auto containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        
        // Get a reference to a blob
        std::string blobName = "sample-blob";
        auto blobClient = containerClient.GetBlobClient(blobName);
        
        // TODO: perform some action with the blob client
        // auto downloadResult = blobClient.DownloadTo("path/to/local/file");
        
        std::cout << "Successfully authenticated using system-assigned managed identity." << std::endl;
        
    } catch (const std::exception& ex) {
        std::cout << "Exception: " << ex.what() << std::endl;
        return 1;
    }
    
    return 0;
}