使用 Azure SDK for .NET 向 Azure 資源驗證 Azure 裝載的應用程式

若使用 Azure App Service、Azure 虛擬機器或 Azure 容器執行個體等服務將應用程式裝載於 Azure 中,向 Azure 資源驗證應用程式時,建議使用受控識別

受控識別會為您的應用程式提供身分識別,使其能夠連線至其他 Azure 資源,而無須使用秘密金鑰或其他應用程式秘密。 在內部,Azure 知道您應用程式的身分識別,及其能夠連線至哪些資源。 Azure 會使用這項資訊自動取得應用程式的 Microsoft Entra 權杖,使其能夠連線至其他 Azure 資源,且讓您完全無須管理任何應用程式秘密。

受控識別類型

受控身分識別有兩種:

  • 系統指派的受控識別 - 此類型的受控識別由 Azure 資源提供,且兩者直接繫結。 當您在 Azure 資源上啟用受控識別時,您會取得系統為該資源指派的受控識別。 系統指派的受控識別會繫結至與其相關聯之 Azure 資源的生命週期。 因此,當刪除資源時,Azure 會自動為您刪除身分識別。 您只需要為裝載程式碼的 Azure 資源啟用受控識別即可,因此,這可說是最容易使用的受控識別類型。
  • 使用者指派的受控識別 - 您也可以將受控識別建立為獨立 Azure 資源的形式。 您的解決方案有多個工作負載執行於多個 Azure 資源上,且這些資源全都需要共用相同的身分識別和相同的權限時,最常用到此身分識別。 例如,如果您的解決方案有元件執行於多個 App Service 和虛擬機器執行個體上,而這些執行個體全都需要存取相同的 Azure 資源集,則在這些資源間建立並使用由使用者指派的受控識別,將是有效的做法。

本文將說明為應用程式啟用及使用系統指派的受控識別所需的步驟。 如果您需要使用由使用者指派的受控識別,請參閱管理使用者指派的受控識別一文,以了解如何建立使用者指派的受控識別。

1 - 在裝載應用程式的 Azure 資源中啟用受控識別

第一個步驟是在裝載應用程式的 Azure 資源上啟用受控識別。 例如,如果您使用 Azure App Service 來裝載 .NET 應用程式,則必須為裝載應用程式的 App Service Web 應用程式啟用受控識別。 如果您使用虛擬機器來裝載您的應用程式,則應讓 VM 能夠使用受控識別。

您可以使用 Azure 入口網站或 Azure CLI,讓受控識別用於 Azure 資源。

指示 Screenshot
在 Azure 入口網站中瀏覽至裝載應用程式程式碼的資源。

例如,您可以在頁面頂端的搜尋方塊中輸入資源的名稱,並在對話方塊中加以選取,以瀏覽至該資源。
A screenshot showing how to use the top search bar in the Azure portal to locate and navigate to a resource in Azure.
在資源的頁面上,從左側功能表中選取 [身分識別] 功能表項目。

所有能夠支援受控識別的 Azure 資源都會有 [身分識別] 功能表項目,即便功能表的配置可能稍有不同。
A screenshot showing the location of the Identity menu item in the left-hand menu for an Azure resource.
在 [身分識別] 頁面上:
  1. 將 [狀態] 滑桿變更為 [開啟]
  2. 按一下 [檔案] 。
確認對話方塊會確認您是否要為服務啟用受控識別。 回答 [是],即會為 Azure 資源啟用受控識別。
A screenshot showing how to enable managed identity for an Azure resource on the resource's Identity page.

2 - 將角色指派給受控識別

接下來,您必須判斷應用程式需要哪些角色 (權限),並將受控識別指派給 Azure 中的這些角色。 受控識別可以在資源、資源群組或訂閱範圍獲派角色。 此範例會顯示如何在資源群組範圍內指派角色,因為多數應用程式都會將所有 Azure 資源劃分在單一資源群組中。

指示 Screenshot
用 Azure 入口網站頂端的搜尋方塊,搜尋資源群組的名稱,藉此找出應用程式的資源群組。

選取對話方塊中 資源群組 標題下方的資源群組名稱,瀏覽至您的資源群組。
A screenshot showing how to use the top search bar in the Azure portal to locate and navigate to a resource group in Azure. This is the resource group that you'll assign roles (permissions) to.
在資源群組分頁中,從左側功能表選取 存取控制 (IAM) A screenshot showing the location of the Access control (IAM) menu item in the left-hand menu of an Azure resource group.
存取控制 (IAM) 分頁上:
  1. 選取 [角色指派] 索引標籤。
  2. 從頂端功能表選取 [+ 新增],然後從產生的下拉功能表中選取 [新增角色指派]
A screenshot showing how to navigate to the role assignments tab and the location of the button used to add role assignments to a resource group.
新增角色指派 分頁列出所有可指派至資源群組之角色的清單。
  1. 請善用搜尋方塊,將清單篩選為更易於管理的大小。 此範例顯示如何針對儲存體 Blob 角色進行篩選。
  2. 選取您要指派的角色。
選取 [下一步] 前往下一個畫面。
A screenshot showing how to filter and select role assignments to be added to the resource group.
下一個 新增角色指派 分頁能讓您指定要指派角色的使用者。
  1. 選取 [指派存取權] 下的 [受控識別]
  2. 選取 [成員] 下的 [+ 選取成員]
對話方塊會在 Azure 入口網站的右側開啟。
A screenshot showing how to select managed identity as the type of user you want to assign the role (permission) on the add role assignments page.
在 [選取受控識別] 對話方塊中:
  1. [受控識別] 下拉式清單和 [選取] 文字方塊可用來篩選訂閱中的受控識別清單。 此範例選取了 App Service,因此只會顯示與 App Service 相關聯的受控識別。
  2. 為裝載應用程式的 Azure 資源選取受控識別。
選取對話方塊底部的 [選取] 來繼續。
A screenshot showing how to use the select managed identities dialog to filter and select the managed identity to assign the role to.
受控識別此時會在 [新增角色指派] 畫面上顯示為已選取。

選取 [檢閱 + 指派] 移至最終分頁,然後再次選取 [檢閱 + 指派] 完成此流程。
A screenshot of the final add role assignment screen where a user needs to select the Review + Assign button to finalize the role assignment.

3 - 在應用程式中實作 DefaultAzureCredential

DefaultAzureCredential 支援多種驗證方法,並會決定執行階段期間使用的驗證方法。 因此,您的應用程式可在相異的環境中使用不同的驗證方法,無須實作環境特定程式碼。

DefaultAzureCredential 尋找認證的順序和位置,可在 DefaultAzureCredential中找到。

為實作 DefaultAzureCredential,請先將 Azure.Identity 與選擇性的 Microsoft.Extensions.Azure 套件新增至您的應用程式。 您可透過命令列或 NuGet 套件管理員執行此動作。

在應用程式專案目錄中開啟您想要的終端環境,然後輸入下列命令。

dotnet add package Azure.Identity
dotnet add package Microsoft.Extensions.Azure

Azure 服務通常使用 SDK 的對應用戶端類別來存取。 這些類別和您自訂的服務都應在 Program.cs 檔案中註冊,使其可在應用程式中透過相依性插入來存取。 請依照下列步驟,在 Program.cs 內正確設定您的服務和 DefaultAzureCredential

  1. 用 Using 陳述式納入 Azure.IdentityMicrosoft.Extensions.Azure 命名空間。
  2. 用相關的協助程式方法註冊 Azure 服務。
  3. DefaultAzureCredential 物件的執行個體傳遞至 UseCredential 方法。

下列程式碼區段示範其中一種範例。

using Microsoft.Extensions.Azure;
using Azure.Identity;

// Inside of Program.cs
builder.Services.AddAzureClients(x =>
{
    x.AddBlobServiceClient(new Uri("https://<account-name>.blob.core.windows.net"));
    x.UseCredential(new DefaultAzureCredential());
});

或者,您也可以更直接地在服務中使用 DefaultAzureCredential,而不求助於其他 Azure 註冊方法,如下所示。

using Azure.Identity;

// Inside of Program.cs
builder.Services.AddSingleton<BlobServiceClient>(x => 
    new BlobServiceClient(
        new Uri("https://<account-name>.blob.core.windows.net"),
        new DefaultAzureCredential()));

上述程式碼在本機開發期間於本機工作站上執行時,會尋找環境變數中的應用程式服務主體,或在 Visual Studio、VS Code、Azure CLI 或 Azure PowerShell 中尋找一組開發人員認證;任意者皆可於本機開發期間用來向 Azure 資源驗證應用程式。

部署至 Azure 時,這同一組程式碼也可向其他 Azure 資源驗證您的應用程式。 DefaultAzureCredential 可擷取環境設定與受控識別設定,以便自動向其他服務進行驗證。