使用開發人員帳戶,在本機開發期間向 Azure 服務驗證 .NET 應用程式

建立雲端應用程式時,開發人員必須在本機工作站上偵錯及測試應用程式。 當應用程式在本機開發期間於開發人員的工作站上執行時,仍必須向應用程式使用的任何 Azure 服務進行驗證。 本文涵蓋如何使用開發人員的 Azure 認證,在本機開發期間向 Azure 驗證應用程式。

A diagram showing how an app running in local developer will obtain the application service principal from a .env file and then use that identity to connect to Azure resources.

若要讓應用程式在使用開發人員 Azure 認證進行本機開發期間向 Azure 進行驗證,開發人員必須從 VS Code Azure Tools 延伸模組、Azure CLI 或 Azure PowerShell 登入 Azure。 Azure SDK for .NET 能夠偵測開發人員是否從其中一個工具登入,然後從認證快取獲得必要的認證,以透過登入使用者的身分向 Azure 驗證應用程式。

這種方法對開發小組而言最容易設定,因為使用的是開發人員現有 Azure 帳戶。 不過,開發人員帳戶擁有的權限可能比應用程式需要的還多,因此超出應用程式在實際執行環境中執行時的權限。 您可以改為建立在本機開發期間使用的應用程式服務主體,設定其權限範圍與應用程式需要的存取權相符。

1 - 建立 Azure AD 群組以進行本機開發

由於應用程式工作幾乎都需要多名開發人員,因此建議您先建立 Azure AD 群組,以封裝應用程式在本機開發中需要的角色 (權限)。 這具有以下優點。

  • 由於角色是在群組層級指派,因此指派給每位開發人員的角色都能保證相同。
  • 如果應用程式需要新角色,只要在應用程式的 Azure AD 群組新增即可。
  • 如果新的開發人員加入小組,他們只需要加入正確的 Azure AD 群組,就能取得正確的權限來處理應用程式。

如果您的開發小組已經有 Azure AD 群組,您可以使用該群組。 否則,請完成下列步驟以建立 Azure AD 群組。

指示 Screenshot
在頁面頂端的搜尋方塊中鍵入 Azure Active Directory,然後選取服務下方的 [Azure Active Directory],瀏覽到 Azure 入口網站中的 Azure Active Directory 頁面。 A screenshot showing how to use the top search bar in the Azure portal to search for and navigate to the Azure Active Directory page.
Azure Active Directory 頁面中,選取左側功能表內的 [群組] A screenshot showing the location of the Groups menu item in the left-hand menu of the Azure Active Directory Default Directory page.
在 [所有群組] 頁面中,選取 [新增群組] A screenshot showing the location of the New Group button in the All groups page.
在 [新增群組] 頁面進行下列設定:
  1. [群組類型] → [安全性]
  2. [群組名稱] → 安全性群組的名稱,通常從應用程式名稱建立。 在群組名稱中包含 local-dev 這樣的字串也很有幫助,能夠指出群組的用途。
  3. [群組描述] → 群組用途的描述。
  4. 選取 [成員] 下方的 [未選取任何成員] 連結,為群組新增成員。
A screenshot showing how to fill out the form to create a new Azure Active Directory group for the application. This screenshot also shows the location of the link to select to add members to this group
在 [新增成員] 對話方塊進行下列設定:
  1. 使用搜尋方塊篩選清單中的使用者名稱清單。
  2. 選取此應用程式的本機開發使用者。 選取物件後,物件會移至對話方塊底部 [選取的項目] 清單。
  3. 完成後,選取 [選取] 按鈕。
A screenshot of the Add members dialog box showing how to select developer accounts to be included in the group.
回到 [新增群組] 頁面,選取 [建立] 以建立群組。

群組隨即建立,而您會回到 [所有群組] 頁面。 群組最多可能需 30 秒才會顯示,且礙於 Azure 入口網站中的快取,您可能必須重新整理頁面才能看見。
A screenshot of the New Group page showing how to complete the process by selecting the Create button.

2 - 將角色指派給 Azure AD 群組

接著,您必須決定應用程式針對哪些資源需要哪些角色 (權限),並將這些角色指派給應用程式。 在此範例中,角色會指派給步驟 1 建立的 Azure Active Directory 群組。 角色可在資源、資源群組或訂閱範圍內獲派其他角色。 此範例顯示如何在資源群組範圍內指派角色,因為多數應用程式都會將所有 Azure 資源劃分在單一資源群組中。

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

選取對話方塊中 資源群組 標題下方的資源群組名稱,瀏覽至您的資源群組。
A screenshot showing how to use the top search box in the Azure portal to locate and navigate to the resource group you want to assign roles (permissions) to.
在資源群組分頁中,從左側功能表選取 存取控制 (IAM) A screenshot of the resource group page showing the location of the Access control (IAM) menu item.
存取控制 (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 the radio button to select to assign a role to an Azure AD group and the link used to select the group to assign the role to.
在 [選取成員] 對話方塊進行下列設定:
  1. [選取] 文字輸入框可用來篩選訂閱中的使用者及群組清單。 如有需要,請鍵入您為應用程式建立之本機開發 Azure AD 群組的前幾個字元。
  2. 選取與您應用程式相關聯的本機開發 Azure AD 群組。
選取對話方塊底部的 [選取] 來繼續。
A screenshot showing how to filter for and select the Azure AD group for the application in the Select members dialog box.
現在,Azure AD 群組會在 [新增角色指派] 畫面上顯示為已選取。

選取 [檢閱 + 指派] 移至最終分頁,然後再次選取 [檢閱 + 指派] 完成此流程。
A screenshot showing the completed Add role assignment page and the location of the Review + assign button used to complete the process.

3 - 使用 .NET 工具登入 Azure

接下來,您需要使用其中一個 .NET 工具選項登入 Azure。 您登入的帳戶也應該存在於您稍早建立和設定的 Azure Active Directory 群組中。

在 Visual Studio 的頂端功能表,瀏覽到 [工具]>[選項],以開啟選項對話方塊。 在左上方的搜尋列中,鍵入 Azure 來篩選選項。 在 [Azure 服務驗證] 下方,選擇 [帳戶選取]

選取 [選擇帳戶] 下方的下拉式功能表,然後選擇新增 Microsoft 帳戶。 視窗隨即開啟,提示您挑選帳戶。 輸入您要使用的 Azure 帳戶認證,然後選取確認。

A screenshot showing how to sign in to Azure using Visual Studio.

4 - 在應用程式中實作 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 可擷取環境設定與受控識別設定,以便自動向其他服務進行驗證。