你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为 Azure VM 上的 SQL Server 启用 Microsoft Entra 身份验证

Applies to:SQL Server on Azure VM

本文介绍如何为 Azure 虚拟机 (VM) 上的 SQL Server 启用 Microsoft Entra ID(以前称为 Azure Active Directory)身份验证。

Note

It's possible to configure Microsoft Entra authentication for unregistered instances of SQL Server, such as when you have multiple SQL Server instances on the same VM.

Overview

从 SQL Server 2022 开始,可以使用以下 Microsoft Entra 身份验证方法之一连接到 Azure VM 上的 SQL Server:

  • Password offers authentication with Microsoft Entra credentials
  • 通用且具有 MFA”添加多重身份验证
  • Integrated uses federation providers like Active Directory Federation Services (ADFS) to enable single sign-on (SSO) experiences
  • Service Principal enables authentication from Azure applications
  • Managed Identity enables authentication from applications assigned Microsoft Entra identities

为 SQL Server 创建 Microsoft Entra 登录名以及用户使用该登录名连接时,SQL Server 使用托管标识来查询 Microsoft Graph。 为 Azure VM 上的 SQL Server 启用 Microsoft Entra 身份验证时,需要提供 SQL Server 可用于与 Microsoft Entra ID 通信的托管标识。 此托管标识需要具有查询 Microsoft Graph 的权限。

When enabling a managed identity for a resource in Azure, the security boundary of the identity is the resource to which it's attached. 例如,启用了 Azure 资源托管标识的虚拟机的安全边界是虚拟机。 在该 VM 上运行的任何代码都可以调用托管标识终结点和请求令牌。 为 Azure VM 上的 SQL Server 启用托管标识时,该标识会附加到虚拟机,因此安全边界是虚拟机。 在使用支持托管标识的其他资源时,体验是相似的。 有关详细信息,请阅读托管标识常见问题解答

使用 Azure VM 上的 SQL Server 进行 Microsoft Entra 身份验证的系统分配和用户分配的托管标识具有以下优势:

  • 系统分配的托管标识提供了简化的配置过程。 由于托管标识的生存期与虚拟机相同,因此在删除虚拟机时无需单独删除它。
  • 用户分配的托管标识提供可伸缩性,因为它可以附加到 Azure VM 上的多个 SQL Server,并用于 Microsoft Entra 身份验证。

若要开始使用托管标识,请查看使用 Azure 门户配置托管标识

Prerequisites

要在 SQL Server 上启用 Microsoft Entra 身份验证,需要满足以下先决条件:

Grant permissions

对于你为了方便进行 SQL Server 与 Microsoft Entra ID 之间的身份验证而选择的托管标识,必须具有以下三种 Microsoft Graph 应用程序权限(应用角色):User.Read.AllGroupMember.Read.AllApplication.Read.All

或者,将托管标识添加到“Microsoft Entra 目录读取者角色”就可授予足够的权限。 Another way to assign the Directory Readers role to a managed identity is to assign the Directory Readers role to a group in Microsoft Entra ID. 然后,组所有者可以将虚拟机托管标识添加为此组的成员。 这将最大限度地减少对 Microsoft Entra 角色管理员的参与,并将责任委托给群组所有者。

将托管标识添加到角色

This section explains how to add your managed identity to the Directory Readers role in Microsoft Entra ID. 需要具有特权角色管理员权限才能更改目录读取者角色分配。 如果没有足够的权限,请与 Microsoft Entra 管理员合作执行以下步骤。

To grant your managed identity the Directory Readers role, follow these steps:

  1. 在 Azure 门户中打开 Microsoft Entra ID 角色和管理员

  2. Type Directory readers in the search box, and then select the role Directory readers to open the Directory Readers | Assignments page:

    Azure 门户的“角色和管理员”页面的屏幕截图,其中搜索并选择了“目录读取者”角色。

  3. 在“目录读取者 | 分配”页面上,选择“+ 添加分配”以打开“添加分配”页面。

    Azure 门户的“目录读取者”页的屏幕截图。

  4. On the Add assignments page, choose No member selected under Select members to open the Select a member page.

    Azure 门户的“添加分配”页的屏幕截图,其中突出显示了“未选择成员”。

  5. 在“选择成员”页面上,搜索要用于 SQL Server VM 的托管标识的名称并将其添加到“目录读取者”角色。 对于系统分配的托管标识,搜索 VM 名称。 Use Select to confirm the identity and go back to the Add assignments page.

    在 Azure 门户中搜索要选择的成员的屏幕截图。

  6. Verify that you see your chosen identity under Select members and then select Next.

    Azure 门户中“添加分配”页的屏幕截图,其中 VM2 已添加为分配。

  7. Verify that your assignment type is set to Active and the box next to Permanently assigned is checked. 输入业务理由,例如将目录读取者角色权限添加到 VM2 的系统分配的标识,然后选择“分配”以保存设置并返回到“目录读取者 | 分配”页面。

    Azure 门户中“添加分配”设置的屏幕截图。

  8. 在“目录读取者 | 分配”页面,确认在“目录读取者”下看到了新添加的标识。

    Azure 门户的“目录读取者”页的屏幕截图,其中显示了已添加到角色的 VM 分配。

添加应用角色权限

You can use Azure PowerShell to grant app roles to a managed identity. 为此,请执行下列步骤:

Note

自 2024 年 3 月 30 日起,Azure AD 和 MSOnline PowerShell 模块已弃用。 To learn more, read the deprecation update. 在此日期之后,对这些模块的支持仅限于到 Microsoft Graph PowerShell SDK 的迁移帮助和安全性修复。 弃用的模块将持续运行至 2025 年 3 月 30 日。

我们建议迁移到 Microsoft Graph PowerShell,以便与 Microsoft Entra ID(以前称为 Azure AD)进行交互。 For common migration questions, refer to the Migration FAQ. Note: Versions 1.0.x of MSOnline may experience disruption after June 30, 2024.

  1. 连接到 Microsoft Graph

    Connect-MgGraph -Scopes "AppRoleAssignment.ReadWrite.All" -TenantId "<tenant id>"
    
  2. 检索托管标识:

    $Graph_SP = Get-MgServicePrincipal -Filter "DisplayName eq 'Microsoft Graph'"
    $MSI = Get-MgServicePrincipal -Filter "displayName eq '<your managed identity display name>'"
    
  3. User.Read.All 角色分配给标识:

    $AAD_AppRole = $Graph_SP.AppRoles | Where-Object {$_.Value -eq "User.Read.All"}  
    New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $MSI.Id -BodyParameter @{principalId=$MSI.Id; resourceId=$Graph_SP.Id; appRoleId=$AAD_AppRole.Id}
    
  4. GroupMember.Read.All 角色分配给标识:

    $AAD_AppRole = $Graph_SP.AppRoles | Where-Object {$_.Value -eq "GroupMember.Read.All"}  
    New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $MSI.Id -BodyParameter @{principalId=$MSI.Id; resourceId=$Graph_SP.Id; appRoleId=$AAD_AppRole.Id}
    
  5. Application.Read.All 角色分配给标识:

    $AAD_AppRole = $Graph_SP.AppRoles | Where-Object {$_.Value -eq "Application.Read.All"}  
    New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $MSI.Id -BodyParameter @{principalId=$MSI.Id; resourceId=$Graph_SP.Id; appRoleId=$AAD_AppRole.Id}
    

可以通过执行以下操作来验证是否已将权限分配给托管标识:

  1. Azure 门户中,转到 Microsoft Entra ID。
  2. Choose Enterprise applications and then select All applications under Manage.
  3. Filter the Application type by Managed identities.
  4. Select the managed identity and then choose Permissions under Security. 你应该会看到以下权限:User.Read.AllGroupMember.Read.AllApplication.Read.All

启用出站通信

要使 Microsoft Entra 身份验证正常工作,需要满足以下条件:

  • 从 SQL Server 到 Microsoft Entra ID 和 Microsoft Graph 终结点的出站通信。
  • 从 SQL 客户端到 Microsoft Entra ID 的出站通信。

使用默认 Azure VM 配置,可与 Microsoft Graph 终结点以及 Microsoft Entra ID 进行出站通信,但某些用户选择使用 OS 级别防火墙或 Azure VNet 网络安全组 (NSG) 来限制出站通信。

SQL Server VM 上的防火墙和任何 SQL 客户端都需要允许端口 80 和 443 上的出站流量。

托管 SQL Server VM 的 VNet 的 Azure VNet NSG 规则应满足以下条件:

  • 具有 AzureActiveDirectory 的服务标记。
  • 目标端口范围:80、443。
  • Action set to Allow.
  • 高优先级(一个较小的数字)。

为已注册实例启用Microsoft Entra 身份验证

使用 SQL IaaS 代理扩展注册 SQL Server 实例时,可以使用 Azure 门户、Azure CLI 或 PowerShell 为已注册的实例启用Microsoft Entra 身份验证。 仅在已注册的 SQL Server 实例上支持使用 Azure 门户或 Azure CLI 管理实例。

Note

启用Microsoft Entra 身份验证后,可以按照本节中的相同步骤更新配置以使用不同的托管标识。

要为 SQL Server VM 启用 Microsoft Entra 身份验证,请执行以下步骤:

  1. 在 Azure 门户中导航到 SQL 虚拟机资源

  2. Select Security Configuration under Security.

  3. Choose Enable under Microsoft Entra authentication.

  4. Choose the managed identity type from the drop-down, either System-assigned or User-assigned. 如果选择用户分配的标识,则从显示的“用户分配的托管标识”下拉列表中,选择要用于对 Azure VM 上的 SQL Server 进行身份验证的标识。

    屏幕截图,其中显示了 Azure 门户中 SQL VM 的安全配置页,页面上“Microsoft Entra 身份验证”被选中。

启用Microsoft Entra 身份验证后,可以按照相同的步骤更改哪些托管标识可以向 SQL Server VM 进行身份验证。

Note

错误 The selected managed identity does not have enough permissions for Microsoft Entra authentication 指示未正确将权限分配给所选标识。 Check the Grant permissions section to assign proper permissions.

为未注册的实例启用Microsoft Entra 身份验证

如果 SQL Server 实例未注册到 SQL IaaS 代理扩展,例如在同一 VM 上有多个 SQL Server 实例时,可以使用 PowerShell 启用Microsoft Entra 身份验证。 可以为特定未注册的实例或 VM 上的所有实例启用Microsoft Entra 身份验证。

Note

若要对 Azure VM 上的 SQL Server 上的未注册实例使用 Microsoft Entra 身份验证,必须至少有一个实例已注册到 SQL IaaS 代理扩展

使用 Set-AzVMExtension -ExtensionName "SqlIaasExtension" 命令为 SQL Server 实例启用 Microsoft Entra 身份验证时,请考虑以下事项:

  • 仅当 CheckPermissions 参数设置为 true 时,才会检查托管标识的权限。
  • 指定标识 ClientID 参数的客户端 ID 以使用 用户分配的托管标识ClientID当参数为空时,将使用系统分配的托管标识
  • 提供参数中的 EnableForGivenInstances 实例列表,以便为特定未注册的实例启用Microsoft Entra 身份验证。 Othewrise,使用 EnableForAllInstances 参数为虚拟机上所有未注册的实例启用 Microsoft Entra 身份验证。

The following example enables Microsoft Entra authentication for all instances on the VM using a system-assigned identity:

Set-AzVMExtension -ExtensionName "SqlIaasExtension" -ResourceGroupName $resourceGroupName -VMName $sqlVMName -Publisher "Microsoft.SqlServer.Management" -ExtensionType "SqlIaaSAgent" -TypeHandlerVersion 2.0 -Location $region -SettingString '{"ServerConfigurationsManagementSettings": {"AADAuthenticationSettings": {"ClientId":"", "EnableForAllInstances":true, "CheckPermissions":true}},"DeploymentTokenSettings":{"DeploymentToken":12345678}}'

The following example enables Microsoft Entra authentication for specific unregistered instances using a user-assigned identity:

Set-AzVMExtension -ExtensionName "SqlIaasExtension" -ResourceGroupName $resourceGroupName -VMName $sqlVMName -Publisher "Microsoft.SqlServer.Management" -ExtensionType "SqlIaaSAgent" -TypeHandlerVersion 2.0 -Location $region -SettingString '{"ServerConfigurationsManagementSettings": {"AADAuthenticationSettings": {"ClientId":$clientId, "EnableForGivenInstances":["MSSQLSERVER","MSSQLSERVER01"], "CheckPermissions":true}},"DeploymentTokenSettings":{"DeploymentToken":12345678}}'

创建登录名和用户

按照 Microsoft Entra 教程 中的步骤为托管标识创建登录名和用户。

连接到实例

按照 Microsoft Entra 教程 中的说明使用 Microsoft Entra 身份验证连接到 SQL Server 实例。

Limitations

请考虑以下限制:

  • 只有已向 SQL IaaS 代理扩展注册并部署到任何云的 Windows VM 上运行的 SQL Server 2022 才支持 Microsoft Entra 身份验证。
  • 在 Azure 门户中管理 Microsoft Entra 身份验证仅适用于 SQL IaaS 代理扩展支持的实例,例如默认实例或单个命名实例。 使用 Azure CLI 或 PowerShell 管理未注册到 SQL IaaS 代理扩展的 SQL Server VM 上的 Microsoft Entra 身份验证其他实例。
  • 不支持对故障转移群集实例使用 Microsoft Entra 身份验证。
  • The identity you choose to authenticate to SQL Server has to have either the Directory Readers role in Microsoft Entra ID or the following three Microsoft Graph application permissions (app roles): User.Read.All, GroupMember.Read.All, and Application.Read.All.
  • 启用 Microsoft Entra 身份验证后,无法将其禁用。
  • Currently, authenticating to SQL Server on Azure VMs through Microsoft Entra authentication using the FIDO2 method isn't supported.

Next steps

Review the security best practices for SQL Server.

有关其他与在 Azure VM 中运行 SQL Server 相关的文章,请参阅 Azure 虚拟机上的 SQL Server 概述。 如果对 SQL Server 虚拟机有任何疑问,请参阅常见问题解答

有关详细信息,请参阅本最佳做法系列中的其他文章: