Compartir a través de

为 Azure 文件共享启用 Active Directory 域服务身份验证

适用于: ✔️ SMB Azure 文件共享

本文介绍在存储帐户上启用 Active Directory 域服务(AD DS)身份验证的过程。 通过启用此身份验证方法,可以使用本地 Active Directory (AD) 凭据向 Azure 文件共享进行身份验证。

重要

在启用 AD DS 身份验证之前,请确保了解概述文章中支持的方案和要求并完成必要的先决条件。 如果你的 Active Directory 环境跨多个林,请参阅将 Azure 文件存储用于多个 Active Directory 林

若要通过 SMB 为 Azure 文件共享启用 AD DS 身份验证,请将 Azure 存储帐户注册到本地 AD DS,然后在存储帐户上设置所需的域属性。 若要将存储帐户注册到 AD DS,请在 AD DS 中创建表示该帐户的计算机帐户(或服务登录帐户)。 将此过程视为类似于在 AD DS 中创建一个表示本地 Windows 文件服务器的帐户。 在存储帐户上启用该功能时,它适用于帐户中的所有新文件共享和现有文件共享。

AzFilesHybrid PowerShell 模块提供用于部署和配置 Azure 文件存储的 cmdlet。 它包括用于将存储帐户加入到您本地 Active Directory 域服务的 cmdlet,以及配置您 DNS 服务器的 cmdlet。 这些 cmdlet 为你进行必要的修改并启用此功能。 由于某些 cmdlets 的部分会与您的本地 AD DS 交互,请查看这些 cmdlets 的作用说明。 然后,可以确定这些更改是否符合合规性和安全策略,并确保你具有执行 cmdlet 的适当权限。 虽然我们建议使用 AzFilesHybrid 模块,但如果无法使用它,请参阅 手动步骤

重要

AES-256 Kerberos 加密是 AzFilesHybrid 模块支持的唯一加密方法。 如果以前使用旧版 AzFilesHybrid 版本(低于 v0.2.2)启用该功能,则使用 RC4 作为默认加密方法,请立即更新到 AES-256。 有关详细信息,请参阅 排查 Azure 文件 SMB 身份验证问题

先决条件

下载 AzFilesHybrid 模块

下载并解压缩 AzFilesHybrid 模块的最新版本

运行 Join-AzStorageAccount

Join-AzStorageAccount cmdlet 对指定的存储帐户执行脱机域联接等效操作。 以下脚本使用此 cmdlet 在 AD 域中创建 计算机帐户 。 如果无法使用计算机帐户,可以更改脚本以改为创建 服务登录帐户 。 从 AzFilesHybrid 版本 0.2.5 开始,支持对服务登录帐户使用 AES-256 加密。

cmdlet 创建的 AD DS 帐户表示存储帐户。 如果在强制密码过期的组织单位(OU)下创建 AD DS 帐户,则必须在最长密码期限之前更新密码。 如果未在该日期之前更新帐户密码,则访问 Azure 文件共享时身份验证会失败。 有关如何更新密码的详细信息,请参阅 “更新 AD DS 帐户密码”。

重要

Join-AzStorageAccount cmdlet 创建一个 AD 帐户来表示 AD 中的存储帐户(文件共享)。 可以选择注册为计算机帐户或服务登录帐户。 有关详细信息,请参阅 常见问题解答。 如果服务登录帐户密码在 AD 域或 OU 上设置了默认密码过期时间,则可以在 AD 中过期。 由于计算机帐户密码更改由客户端计算机而非 AD 驱动,因此它们不会在 AD 中过期,尽管客户端计算机默认每 30 天更改一次密码。 对于这两种帐户类型,请检查密码过期期限,并计划在最长密码期限之前更新 AD 帐户 的存储帐户标识的密码 。 可以考虑在 AD 中创建新的 AD 组织单位,并相应地在计算机帐户或服务登录帐户上禁用密码过期策略。

在已加入本地 AD DS 的设备上,在 PowerShell 5.1 中运行以下脚本。 使用有权在目标 AD 中创建计算机帐户或服务登录帐户的本地 AD DS 凭据(例如域管理员)。 若要遵循最低特权原则,本地 AD DS 凭据必须具有以下 Azure 角色:

  • 目标存储帐户所在的资源组上的“读取者”。
  • AD DS 中用于联接存储帐户的“贡献者”角色。

如果用于在 AD DS 中加入存储帐户的帐户是目标资源的 Azure 订阅中的 所有者参与者 ,则帐户已启用以执行加入,不需要进一步分配。

AD DS 凭据还必须有权在目标 AD 中创建计算机帐户或服务登录帐户。 在运行脚本之前,请将占位符值替换为你自己的值。

# Change the execution policy to unblock importing AzFilesHybrid.psm1 module
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

# Navigate to where AzFilesHybrid is unzipped and stored and run to copy the files into your path
.\CopyToPSPath.ps1 

# Import AzFilesHybrid module
Import-Module -Name AzFilesHybrid

# Login to Azure using a credential that has either storage account owner or contributor Azure role assignment.
Connect-AzAccount -Environment AzureChinaCloud

# Define parameters
# $StorageAccountName is the name of an existing storage account that you want to join to AD
# $SamAccountName is the name of the to-be-created AD object, which is used by AD as the logon name 
# for the object. It must be 20 characters or less and has certain character restrictions.
# Certain NETBIOS restrictions might require a $SamAccountName of 15 characters or less.
# Make sure that you provide the SamAccountName without the trailing '$' sign.
# See https://learn.microsoft.com/windows/win32/adschema/a-samaccountname for more information.
$SubscriptionId = "<your-subscription-id-here>"
$ResourceGroupName = "<resource-group-name-here>"
$StorageAccountName = "<storage-account-name-here>"
$SamAccountName = "<sam-account-name-here>"
$DomainAccountType = "<ComputerAccount|ServiceLogonAccount>" # Default is set as ComputerAccount
# If you don't provide the OU name as an input parameter, the AD identity that represents the 
# storage account is created under the root directory.
$OuDistinguishedName = "<ou-distinguishedname-here>"
# Encryption method is AES-256 Kerberos.

# Select the target subscription for the current session
Select-AzSubscription -SubscriptionId $SubscriptionId 

# Register the target storage account with your active directory environment under the target OU 
# (for example: specify the OU with Name as "UserAccounts" or DistinguishedName as 
# "OU=UserAccounts,DC=CONTOSO,DC=COM"). You can use this PowerShell cmdlet: Get-ADOrganizationalUnit 
# to find the Name and DistinguishedName of your target OU. If you are using the OU Name, specify it 
# with -OrganizationalUnitName as shown below. If you are using the OU DistinguishedName, you can set it 
# with -OrganizationalUnitDistinguishedName. You can choose to provide one of the two names to specify 
# the target OU. You can choose to create the identity that represents the storage account as either a 
# Service Logon Account or Computer Account (default parameter value), depending on your AD permissions 
# and preference. Run Get-Help Join-AzStorageAccountForAuth for more details on this cmdlet.

Join-AzStorageAccount `
        -ResourceGroupName $ResourceGroupName `
        -StorageAccountName $StorageAccountName `
        -SamAccountName $SamAccountName `
        -DomainAccountType $DomainAccountType `
        -OrganizationalUnitDistinguishedName $OuDistinguishedName

# You can run the Debug-AzStorageAccountAuth cmdlet to conduct a set of basic checks on your AD configuration 
# with the logged on AD user. This cmdlet is supported on AzFilesHybrid v0.1.2+ version. For more details on 
# the checks performed in this cmdlet, see Azure Files Windows troubleshooting guide.
Debug-AzStorageAccountAuth -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName -Verbose

选项 2:手动执行启用操作

大多数客户应选择 选项 1 并使用 AzFilesHybrid PowerShell 模块通过 Azure 文件启用 AD DS 身份验证。 但是,如果希望使用 Active Directory PowerShell 手动执行这些步骤,此处概述了这些步骤。

重要

如果已成功运行 Join-AzStorageAccount 脚本,请转到 “确认功能已启用” 部分。 不需要执行以下手动步骤。

检查环境

首先,检查你的环境状态。

  • 检查 是否已安装 Active Directory PowerShell ,以及 shell 是否以管理员权限运行。
  • 确保 Az.Storage 模块 已安装,如果未安装,请安装。 至少需要版本 2.0。
  • 完成这些检查后,请检查 AD DS,以查看是否存在使用 SPN/UPN 创建的 计算机帐户 (默认)或服务 登录帐户 ,例如“cifs/your-storage-account-name-here.file.core.chinacloudapi.cn”。 如果帐户不存在,请按照下一节的说明创建一个帐户。

重要

必须在 PowerShell 5.1 的本节中运行 Windows Server Active Directory PowerShell cmdlet。 在此方案中,PowerShell 7.x 不起作用。

在 AD 中手动创建可表示存储帐户的标识

若要手动创建此帐户,请先为存储帐户创建新的 Kerberos 密钥,并使用以下 PowerShell cmdlet 获取访问密钥。 仅在安装过程中使用此密钥。 您不能将其用于针对存储帐户的任何控制平面或数据平面操作。

# Create the Kerberos key on the storage account and get the Kerb1 key as the password for the AD identity 
# to represent the storage account
$ResourceGroupName = "<resource-group-name-here>"
$StorageAccountName = "<storage-account-name-here>"

New-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -KeyName kerb1
Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ListKerbKey | where-object{$_.Keyname -contains "kerb1"}

cmdlet 会返回键值。 获得 kerb1 密钥后,在 OU 下的 AD 中创建计算机帐户服务帐户,并将该密钥用作 AD 标识的密码。

  1. 将 SPN 设置为 cifs/your-storage-account-name-here.file.core.chinacloudapi.cn ,或者在 Windows 命令行中以管理员身份运行 Setspn 命令(将示例文本替换为存储帐户名称和 <ADAccountName> AD 帐户名称)。

    Setspn -S cifs/your-storage-account-name-here.file.core.chinacloudapi.cn <ADAccountName>
    
  2. 如果你有用户账户,请修改 UPN 以匹配 AD 对象的 SPN(必须安装 AD PowerShell cmdlet,并在 PowerShell 5.1 中使用提升的权限执行 cmdlet)。

    Set-ADUser -Identity $UserSamAccountName -UserPrincipalName cifs/<StorageAccountName>.file.core.chinacloudapi.cn@<DNSRoot>
    

    重要

    不要同步具有无效 userPrincipalName(UPN) 值的用户。 UPN 不得包含特殊字符,如 /空格或其他不受支持的符号。 尝试将无效的 UPN 用户(例如用户名包含 /)进行同步,会导致 Microsoft Entra Connect 错误。 如果此类标识存在于本地目录中,请选择以下之一进行操作:

    • 将 UPN 更新为有效的格式(例如 user@domain.com)。
    • 通过在 Microsoft entra Connect 中使用筛选规则从同步中排除用户。
  3. 将 AD 帐户密码设置为 kerb1 密钥的值。

    Set-ADAccountPassword -Identity servername$ -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "kerb1_key_value_here" -Force)
    

如果你的 OU 强制执行密码过期,则必须在密码过期前更新密码,以防在访问 Azure 文件共享时身份验证失败。 有关详细信息,请参阅 在 AD 中更新存储帐户标识的密码

保留新创建标识的 SID,你在下一步中将用到它。 创建的表示存储帐户的标识无需同步到 Microsoft Entra ID。

在存储帐户上启用此功能

修改以下命令以包含域属性的配置详细信息,然后运行它以启用该功能。 以下命令中所需的存储帐户 SID 是你在上一节的 AD DS 中创建的标识的 SID。 请确保提供 ActiveDirectorySamAccountName 属性时去掉尾随的 ' 符号。

# Set the feature flag on the target storage account and provide the required AD domain information
Set-AzStorageAccount `
        -ResourceGroupName "<your-resource-group-name>" `
        -Name "<your-storage-account-name>" `
        -EnableActiveDirectoryDomainServicesForFile $true `
        -ActiveDirectoryDomainName "<your-domain-dns-root>" `
        -ActiveDirectoryNetBiosDomainName "<your-domain-dns-root>" `
        -ActiveDirectoryForestName "<your-forest-name>" `
        -ActiveDirectoryDomainGuid "<your-guid>" `
        -ActiveDirectoryDomainsid "<your-domain-sid>" `
        -ActiveDirectoryAzureStorageSid "<your-storage-account-sid>" `
        -ActiveDirectorySamAccountName "<your-domain-object-sam-account-name>" `
        -ActiveDirectoryAccountType "<your-domain-object-account-type, the value could be 'Computer' or 'User'>"

若要启用 AES-256 加密,请按照本部分中的步骤操作。

重要

若要启用 AES-256 加密,表示存储帐户的域对象必须是 Active Directory 域中的计算机帐户(默认)或服务登录帐户。 如果域对象不满足此要求,请将其删除并创建符合要求的全新域对象。 此外,你必须对对象的 msDS-SupportedEncryptionTypes 属性具有写入访问权限。

用于配置 AES-256 支持的 cmdlet 取决于表示存储帐户的域对象是计算机帐户或服务登录帐户(用户帐户)。 无论哪种方式,都必须安装 AD PowerShell cmdlet,并以提升的权限在 PowerShell 5.1 中执行 cmdlet。

要在计算机帐户上启用 AES-256 加密,请运行以下命令。 将 <domain-object-identity><domain-name> 替换为自定义值。

Set-ADComputer -Identity <domain-object-identity> -Server <domain-name> -KerberosEncryptionType "AES256"

要在服务登录帐户上启用 AES-256 加密,请运行以下命令。 将 <domain-object-identity><domain-name> 替换为自定义值。

Set-ADUser -Identity <domain-object-identity> -Server <domain-name> -KerberosEncryptionType "AES256"

运行前面的 cmdlet 后,请将以下脚本中的 <domain-object-identity> 替换为你的值,然后运行该脚本来刷新域对象密码:

$KeyName = "kerb1" # Could be either the first or second kerberos key, this script assumes we're refreshing the first
$KerbKeys = New-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -KeyName $KeyName
$KerbKey = $KerbKeys.keys | Where-Object {$_.KeyName -eq $KeyName} | Select-Object -ExpandProperty Value
$NewPassword = ConvertTo-SecureString -String $KerbKey -AsPlainText -Force

Set-ADAccountPassword -Identity <domain-object-identity> -Reset -NewPassword $NewPassword

重要

如果以前使用 RC4 加密并更新了存储帐户以使用 AES-256(建议), klist purge 请在客户端上运行,然后重新装载文件共享以获取 AES-256 的新 Kerberos 票证。

调试

如果需要,请运行 Debug-AzStorageAccountAuth cmdlet 以使用已登录的 AD 用户检查 AD 配置。 AzFilesHybrid v0.1.2+ 版本及更高版本支持此 cmdlet。 此 cmdlet 适用于 AD DS 和 Microsoft Entra Kerberos 身份验证。 它不适用于已启用 Microsoft Entra 域服务的存储帐户。 有关此 cmdlet 执行检查的详细信息,请参阅 无法使用 AD 凭据装载 Azure 文件共享

Debug-AzStorageAccountAuth -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName -Verbose

确认是否已启用此功能

使用以下脚本检查是否已在存储帐户上启用 Active Directory:

# Get the target storage account
$storageaccount = Get-AzStorageAccount `
        -ResourceGroupName "<your-resource-group-name-here>" `
        -Name "<your-storage-account-name-here>"

# List the directory service of the selected service account
$storageAccount.AzureFilesIdentityBasedAuth.DirectoryServiceOptions

# List the directory domain information if the storage account has enabled AD DS authentication for file shares
$storageAccount.AzureFilesIdentityBasedAuth.ActiveDirectoryProperties

如果成功,输出如下所示:

DomainName:<yourDomainHere>
NetBiosDomainName:<yourNetBiosDomainNameHere>
ForestName:<yourForestNameHere>
DomainGuid:<yourGUIDHere>
DomainSid:<yourSIDHere>
AzureStorageID:<yourStorageSIDHere>

重要

必须先 分配共享级别权限,然后才能对用户进行身份验证。

在存储帐户上禁用 AD DS 身份验证

若要使用其他身份验证方法,请使用 Azure 门户、PowerShell 或 Azure CLI 在存储帐户上禁用 AD DS 身份验证。

如果禁用此功能,则在启用和配置其他标识源之一之前,存储帐户中的文件共享不会具有基于标识的访问权限。

重要

在存储帐户上禁用 AD DS 身份验证后,请考虑删除创建的 AD DS 标识(计算机帐户或服务登录帐户),以表示本地 AD 中的存储帐户。 如果将标识保留在 AD DS 中,它将成为一个孤立对象。 删除它不是自动的。

若要使用 Azure 门户在存储帐户上禁用 AD DS 身份验证,请执行以下步骤。

  1. 登录到 Azure 门户,选择要为其禁用 AD DS 身份验证的存储帐户。
  2. 在“数据存储”下,选择“文件共享”。
  3. 基于标识的访问旁边,选择配置状态,确保它是已配置
  4. Active Directory 域服务(AD DS)下,选择“ 配置”。
  5. 选中“ 为此存储帐户禁用 Active Directory ”复选框。
  6. 选择“保存”

下一步