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

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

本文介绍如何在存储帐户上启用Active Directory Domain Services(AD DS)身份验证,以便可以使用 on-premises Active Directory (AD) 凭据对Azure文件共享进行身份验证。

重要

在启用 AD DS 身份验证之前,请阅读 AD DS 概述文章 并完成必要的 先决条件。 如果你的 Active Directory 环境跨多个林,请参阅将 Azure 文件存储用于多个 Active Directory 林

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

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

重要

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

先决条件

下载 AzFilesHybrid 模块

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

运行 Join-AzStorageAccount

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

重要

创建的 Join-AzStorageAccount 帐户表示 AD DS 中的存储帐户。 无论是将其注册为计算机帐户还是服务登录帐户,请检查 AD 域或组织单位(OU)上的密码过期策略。 服务登录帐户密码可以根据默认过期期限过期,而计算机帐户密码更改由客户端计算机(默认情况下每 30 天)驱动,并且不会在 AD 中过期。 对于任一帐户类型,必须在最大密码期限之前更新密码,以避免访问Azure文件共享时身份验证失败。 请考虑 创建新的 AD OU 并在 计算机帐户 或服务登录帐户上相应地禁用密码过期。 有关详细信息,请参阅 常见问题解答

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

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

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

在运行脚本之前,请将占位符值替换为你自己的值。

# 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 here. 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:
# https://learn.microsoft.com/troubleshoot/azure/azure-storage/files/security/files-troubleshoot-smb-authentication#unable-to-mount-azure-file-shares-with-ad-credentials
Debug-AzStorageAccountAuth -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName -Verbose

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

如果无法使用 AzFilesHybrid PowerShell 模块,可以使用 Active Directory PowerShell 手动执行这些步骤。

重要

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

检查环境

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

  • 必须安装 Active Directory PowerShell,并且 shell 必须以管理员权限运行。
  • 必须安装最新版本的 Az.Storage 模块
  • 检查 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. 在 Active Directory GUI 中或通过以管理员身份从 Windows 命令行运行命令,将 SPN 设置为 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。 下一步需要用到它。 创建的表示存储帐户的 AD 标识无需同步到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 域服务的存储帐户。 有关详细信息,请参阅 无法使用 AD 凭据装载Azure文件共享

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

确认是否已启用此功能

使用以下脚本检查 AD DS 是否启用为存储帐户上的标识源。 将 <resource-group-name><storage-account-name> 替换为自定义值。

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

# List the identity source for the selected storage 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. 选择“保存”

下一步