从本地 JavaScript 应用向 Azure 资源进行身份验证

在 Azure 外部运行的应用(例如在本地或第三方数据中心)应使用应用程序服务主体在访问 Azure 资源时向 Azure 进行身份验证。 应用程序服务主体对象是使用 Azure 中的应用注册过程创建的。 创建应用程序服务主体时,将为应用生成客户端 ID 和客户端密码。 在环境变量中存储客户端 ID、客户端密码和租户 ID,以便用于 JavaScript 的 Azure SDK 在运行时使用环境变量向 Azure 验证应用。

应为每个环境(例如测试、阶段、生产)应用运行创建不同的应用注册。 这允许为每个服务主体配置特定于环境的资源权限,并确保部署到一个环境的应用不会与属于另一环境的 Azure 资源通信。

1 - 在 Azure 中注册应用程序

可以使用 Azure 门户或 Azure CLI 向 Azure 注册应用。

登录到 Azure 门户并执行以下步骤。

说明 屏幕快照
在 Azure 门户中:
  1. 在 Azure 门户顶部的搜索栏中输入“应用注册”。
  2. 在搜索栏下方显示的菜单中的“服务”标题下,选择标有“应用注册”的项。
A screenshot showing how to use the top search bar in the Azure portal to find and navigate to the App registrations page.
在“应用注册”页上,选择“+ 新建注册”。 A screenshot showing the location of the New registration button in the App registrations page.
在“注册应用程序”页上,按如下所示填写窗体。
  1. 名称 → 输入该应用注册在 Azure 中的名称。 建议在此名称中包含应用名称和应用注册所针对的环境(测试、生产)。
  2. 支持的帐户类型 → 仅限此组织目录中的帐户。
选择“注册”以注册应用并创建应用程序服务主体。
A screenshot to fill out Register by giving the app a name and specifying supported account types as accounts in this organizational directory only.
在应用的“应用注册”页上:
  1. 应用程序(客户端)ID → 这是应用在本地开发期间用于访问 Azure 的应用 ID。 将此值复制到文本编辑器中的临时位置,因为以后的步骤中需要此值。
  2. 目录(租户)ID →应用在向 Azure 进行身份验证时也需要此值。 将此值复制到文本编辑器中的临时位置,因为在稍后的步骤中也需要用到。
  3. 客户端凭据 → 必须先为应用设置客户端凭据,然后应用才能向 Azure 进行身份验证并使用 Azure 服务。 选择“添加证书或机密”以添加应用的凭据。
A screenshot of the App registration after completion. This screenshot shows the application and tenant IDs, which will be needed in a future step.
在“证书和机密”页上,选择“+ 新建客户端密码”。 A screenshot showing the location of the link to use to create a new client secret on the certificates and secrets page.
页面右侧会弹出“添加客户端机密”对话框。 在此对话框中:
  1. 说明 → 输入值“当前”。
  2. 过期时间 → 选择值“24 个月”。
选择“添加”以添加机密。

重要说明:在日历中设置机密过期日期之前发出的提醒。这样,可以在此机密过期之前添加新机密和更新应用,并避免应用中出现服务中断。
A screenshot showing the page where a new client secret is added for the application service principal created by the app registration process.
证书和机密 ”页显示客户端机密的值。

将此值复制到文本编辑器中的临时位置,因为在将来的步骤中需要它。

重要说明:这是唯一一次看到此值。 离开或刷新此页面后,将无法再次看到此值。 可以添加另一个客户端密码而不使此客户端密码失效,但不会再次看到此值。
A screenshot showing the page with the generated client secret.

2 - 将角色分配到应用程序服务主体

接下来,需要确定应用在哪些资源上需要哪些角色(权限),并将这些角色分配到应用。 可以在资源、资源组或订阅范围分配角色。 此示例演示如何在资源组范围为服务主体分配角色,因为大多数应用程序将其所有 Azure 资源分组到单个资源组中。

说明 屏幕快照
使用 Azure 门户顶部的搜索框搜索资源组名称,找到应用程序的资源组。

在对话框中的“资源组”标题下选择资源组名称,导航到该资源组。
A screenshot showing 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 a Microsoft Entra group and the link used to select the group to assign the role to.
在“选择成员”对话框中
  1. “选择”文本框可用于筛选订阅中的用户和组列表。 如果需要,请键入为应用创建的服务主体的前几个字符以筛选列表。
  2. 选择与应用程序关联的服务主体。
在对话框底部选择“选择”以继续。
A screenshot showing how to filter for and select the Microsoft Entra group for the application in the Select members dialog box.
服务主体在“添加角色分配”屏幕上显示为选中状态。

选择“查看 + 分配”转到最后一页,然后再次选择“查看 + 分配”完成该过程。
A screenshot showing the completed Add role assignment page and the location of the Review + assign button used to complete the process.

3 - 为应用程序配置环境变量

必须为运行 JavaScript 应用的进程设置 AZURE_CLIENT_ID环境变量 AZURE_TENANT_IDAZURE_CLIENT_SECRET 以使应用程序服务主体凭据在运行时可供应用使用。 该 DefaultAzureCredential 对象在这些环境变量中查找服务主体信息。

AZURE_CLIENT_ID=<value>
AZURE_TENANT_ID=<value>
AZURE_CLIENT_SECRET=<value>

4 - 在应用程序中实现 DefaultAzureCredential

若要对 Azure SDK 客户端对象进行身份验证,应用程序应使用 DefaultAzureCredential @azure/标识包中的类。

首先,将 @azure/标识 包添加到应用程序。

npm install @azure/identity

接下来,对于在应用中创建 Azure SDK 客户端对象的任何 JavaScript 代码,需要:

  1. DefaultAzureCredential@azure/identity模块导入类。
  2. 创建 DefaultAzureCredential 对象。
  3. DefaultAzureCredential 对象传递给 Azure SDK 客户端对象构造函数。

以下代码片段中显示了此操作的示例。

// connect-with-default-azure-credential.js
import { BlobServiceClient } from '@azure/storage-blob';
import { DefaultAzureCredential } from '@azure/identity';
import 'dotenv/config'

const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
if (!accountName) throw Error('Azure Storage accountName not found');

const blobServiceClient = new BlobServiceClient(
  `https://${accountName}.blob.core.windows.net`,
  new DefaultAzureCredential()
);

当上述代码实例化 DefaultAzureCredential 对象时, DefaultAzureCredential 读取环境变量 AZURE_SUBSCRIPTION_IDAZURE_TENANT_ID环境变量, AZURE_CLIENT_ID以及 AZURE_CLIENT_SECRET 用于连接到 Azure 的应用程序服务主体信息。