使用 Active Directory 对 Batch 管理解决方案进行身份验证

调用 Azure Batch Management 服务的应用程序使用 Microsoft 身份验证库 (Microsoft Entra ID) 进行身份验证。 Microsoft Entra ID 是 Azure 的基于云的多租户目录和标识管理服务。 Azure 本身使用 Microsoft Entra ID 来对其客户、服务管理员和组织用户进行身份验证。

批处理管理 .NET 库公开用于使用批处理帐户、帐户密钥、应用程序和应用程序包的类型。 Batch Management .NET 库是一个 Azure 资源提供程序客户端,可与 Azure 资源管理器一起使用,以编程方式管理这些资源。 需要使用 Microsoft Entra ID 对通过任何 Azure 资源提供程序客户端(包括 Batch Management .NET 库)和 Azure 资源管理器发出的请求进行身份验证。

本文探讨如何通过 Microsoft Entra ID 在使用 Batch Management .NET 库的应用程序中进行身份验证。 我们将演示如何使用 Microsoft Entra ID 和集成身份验证对订阅管理员或协同管理员进行身份验证。 我们使用 GitHub 上提供的 AccountManagement 示例项目来逐步讲解如何将 Microsoft Entra ID 与 Batch Management .NET 库配合使用。

若要详细了解批处理管理 .NET 库的用法和 AccountManagement 示例,请参阅 Manage Batch accounts and quotas with the Batch Management client library for .NET(使用适用于 .NET 的批处理管理客户端库来管理批处理帐户和配额)。

使用 Microsoft Entra ID 注册应用程序

Microsoft 身份验证库 (MSAL) 提供 Microsoft Entra ID 的编程接口,以便在应用程序中使用。 若要从应用程序调用 MSAL,必须在 Microsoft Entra 租户中注册该应用程序。 注册应用程序时,需要向 Microsoft Entra ID 提供有关该应用程序的信息,包括它在 Microsoft Entra 租户中的名称。 然后,Microsoft Entra ID 将提供一个应用程序 ID,用于在运行时将该应用程序关联到 Microsoft Entra ID。 若要详细信息应用程序 ID,请参阅 Microsoft Entra ID 中的应用程序对象和服务主体对象

若要注册 AccountManagement 示例应用程序,请按照将应用程序与 Microsoft Entra ID 集成添加应用程序部分中的步骤进行操作。 指定“本机客户端应用程序”作为应用程序类型。 用于重定向 URI 的行业标准 OAuth 2.0 URI 是 urn:ietf:wg:oauth:2.0:oob。 但可为重定向 URI 指定任何有效的 URI(例如 http://myaccountmanagementsample),因为它不需要是实际的终结点。

Adding an application

完成注册过程后,将列出应用程序的应用程序 ID 和对象(服务主体)ID。

Completed registration process

向 Azure 资源管理器 API 授予应用程序访问权限

接下来,需要将应用程序的访问权限委派给 Azure 资源管理器 API。 资源管理器 API 的 Microsoft Entra 标识符为 Windows Azure 服务管理 API

在 Azure 门户中执行以下步骤:

  1. 在 Azure 门户的左侧导航窗格中,选择“所有服务”,单击“应用注册”,并单击“添加”。

  2. 在应用注册列表中搜索应用程序名称:

    Search for your application name

  3. 此时会显示“设置”边栏选项卡。 在“API 访问”部分中,选择“所需的权限”。

  4. 单击“添加”添加新的所需权限。

  5. 在步骤 1 中输入 Windows Azure Service Management API,从结果列表中选择该 API,并单击“选择”按钮。

  6. 在步骤 2 中,选中“以组织用户的身份访问 Azure 经典部署模型”旁边的复选框,并单击“选择”按钮。

  7. 单击“完成”按钮。

现在,“所需的权限”边栏选项卡会显示向 MSAL 和资源管理器 API 授予的应用程序权限。 首次在 Microsoft Entra ID 中注册应用时,默认会向 MSAL 授予权限。

Delegate permissions to the Azure Resource Manager API

Microsoft Entra 终结点

若要使用 Microsoft Entra ID 对 Batch Management 解决方案进行身份验证,需要两个已知的终结点。

  • 未提供特定租户时(例如,使用集成身份验证时),Microsoft Entra 通用终结点将提供通用的凭据收集接口:

    https://login.chinacloudapi.cn/common

  • Azure 资源管理器终结点,用于获取对 Batch 管理服务的请求进行身份验证的令牌:

    https://management.core.chinacloudapi.cn/

AccountManagement 示例应用程序定义这些终结点的常量。 请将以下常量保持不变:

// Microsoft Entra ID "common" endpoint.
private const string AuthorityUri = "https://login.chinacloudapi.cn/common";
// Azure Resource Manager endpoint
private const string ResourceUri = "https://management.core.chinacloudapi.cn/";

引用应用程序 ID

在运行时,客户端应用程序使用应用程序 ID(也称为客户端 ID)来访问 Microsoft Entra ID。 在 Azure 门户中注册应用程序后,请更新代码,以使用 Microsoft Entra ID 为已注册的应用程序提供的应用程序 ID。 在 AccountManagement 示例应用程序中,将 Azure 门户上的应用程序 ID 复制到相应的常量中:

// Specify the unique identifier (the "Client ID") for your application. This is required so that your
// native client application (i.e. this sample) can access the Microsoft Graph API. For information
// about registering an application in Microsoft Entra ID, please see "Register an application with the Microsoft identity platform" here:
// https://docs.azure.cn/active-directory/develop/quickstart-register-app
private const string ClientId = "<application-id>";

此外,请复制在注册过程中指定的重定向 URI。 在代码中指定的重定向 URI 必须与注册应用程序时提供的重定向 URI 相匹配。

// The URI to which Microsoft Entra ID will redirect in response to an OAuth 2.0 request. This value is
// specified by you when you register an application with AAD (see ClientId comment). It does not
// need to be a real endpoint, but must be a valid URI (e.g. https://accountmgmtsampleapp).
private const string RedirectUri = "http://myaccountmanagementsample";

获取 Microsoft Entra 身份验证令牌

在 Microsoft Entra 租户中注册 AccountManagement 示例并更新示例源代码中的值后,便可以使用 Microsoft Entra ID 对该示例进行身份验证。 运行该示例时,MSAL 会尝试获取身份验证令牌。 在此步骤中,它会提示你输入 Azure 凭据:

// Obtain an access token using the "common" AAD resource. This allows the application
// to query AAD for information that lies outside the application's tenant (such as for
// querying subscription information in your Azure account).
AuthenticationContext authContext = new AuthenticationContext(AuthorityUri);
AuthenticationResult authResult = authContext.AcquireToken(ResourceUri,
                                                        ClientId,
                                                        new Uri(RedirectUri),
                                                        PromptBehavior.Auto);

提供凭据后,示例应用程序可以继续向批处理管理服务发出身份验证的请求。

后续步骤