使用 Azure SDK for .NET 对访问 Azure 资源的 Azure 托管应用进行身份验证

使用 Azure 应用服务、Azure 虚拟机或 Azure 容器实例等服务将应用托管在 Azure 中时,对访问 Azure 服务的应用进行身份验证的建议方法是使用托管标识

托管标识为应用提供一个标识,使应用无需使用机密密钥或其他应用程序机密即可连接到其他 Azure 资源。 在内部,Azure 知道应用的标识以及应用能够连接到哪些资源。 Azure 使用此信息来自动获取应用的 Microsoft Entra 令牌,使应用能够连接到其他 Azure 资源,你无需管理任何应用程序机密。

托管标识类型

托管标识分为两种类型:

  • 系统分配的托管标识 - 这种类型的托管标识由 Azure 资源提供并直接关联到 Azure 资源。 在 Azure 资源上启用托管标识时,你将获得该资源的系统分配的托管标识。 系统分配的托管标识在与其关联的 Azure 资源的生命周期内有效。 当资源被删除时,Azure 会自动为你删除标识。 由于只需为托管代码的 Azure 资源启用托管标识,因此这种类型的托管标识最容易使用。
  • 用户分配的托管标识 - 还可以创建一个托管标识作为独立的 Azure 资源。 当解决方案具有在多个 Azure 资源上运行的多个工作负载,而所有这些工作负载都需要共享相同的标识和相同的权限时,这种方法最常用。 例如,如果解决方案包含在多个应用服务和虚拟机实例上运行的组件,而所有这些组件都需要访问同一组 Azure 资源,则创建并使用一个针对这些资源的用户分配的托管标识是有效做法。

本文将介绍为应用启用和使用系统分配的托管标识的步骤。 如果需要使用用户分配的托管标识,请参阅管理用户分配的托管标识一文来了解如何创建用户分配的托管标识。

1 - 在托管应用的 Azure 资源中启用托管标识

第一步是在托管应用的 Azure 资源上启用托管标识。 例如,如果使用 Azure 应用服务来托管 .NET 应用程序,则需要为托管应用的应用服务 Web 应用启用托管标识。 如果使用虚拟机来托管应用,请允许 VM 使用托管标识。

可以使用 Azure 门户或 Azure CLI 来启用用于 Azure 资源的托管标识。

说明 屏幕快照
在 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 资源分组到单个资源组中。

说明 屏幕快照
使用 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. 可以使用“托管标识”下拉菜单和“选择”文本框来筛选订阅中的托管标识列表。 此示例选择了“应用服务”,因此只显示与应用服务关联的托管标识。
  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

通常会使用 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 可以检索环境设置和托管标识配置,以自动向其他服务进行身份验证。