在本地开发期间使用开发人员帐户对访问 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 组。

说明 屏幕快照
在页面顶部的搜索框中键入“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 资源分组到单个资源组中。

说明 屏幕快照
使用 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

通常会使用 SDK 中的相应客户端类来访问 Azure 服务。 应在 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 可以检索环境设置和托管标识配置,以自动向其他服务进行身份验证。