如何更改 Microsoft 365 用户默认 MFA 方法
Microsoft 365 用户应使用 Microsoft Authenticator 应用进行双因素身份验证 (2FA),因为它可以防止基于密码的攻击。在 Microsoft Entra ID 中,您可以更新单个用户的默认多重身份验证 (MFA) 方法。但是,如果您想一次为所有用户设置默认 MFA,该怎么办?在本文中,您将了解如何使用 PowerShell 更改所有用户的默认 MFA 方法。
Microsoft 365 多重身份验证
用户可以使用用户名和密码登录其 Microsoft 帐户。然而,密码应该有一个额外的保护层和更安全的身份验证方法。与用户登录时仅使用密码相比,Microsoft 多重身份验证增加了额外的安全性。
为组织启用不同的身份验证方法后,每个用户都可以选择自己喜欢的 MFA 方法。用户必须提供额外的验证,例如批准推送通知、从软件或硬件令牌输入代码,或者回复短信或电话。
如果组织决定对所有用户使用默认身份验证方法,您可以通过两种方式进行更改:
- Microsoft Entra ID(单用户)
- Microsoft Graph PowerShell(单个和所有用户)
更改 Microsoft Entra 中的默认身份验证方法
要更改 Microsoft Entra ID 中单个用户的默认登录方法,请按照下列步骤操作:
- 登录 Microsoft Entra 管理中心
- 展开身份 > 用户 > 所有用户
- 点击用户
- 点击身份验证方法
- 单击警报标志中的箭头 > 单击此处立即使用
- 检查默认登录方法的铅笔图标是否未灰显
注意:如果默认登录方法(预览)呈灰色,则用户没有默认 MFA 或只有一种登录方法。因此,用户需要添加另一种身份验证方法。
- 点击默认登录方法(预览)旁边的铅笔图标
- 选择默认登录方法(Microsoft Authenticator 通知)
- 点击保存
注意:更改用户的默认 MFA 不会删除其他身份验证方法。如果用户有多种身份验证方法,则在默认方法之后,其他方法将成为次要方法。
您已成功更改 Microsoft Entra ID 中单个 Microsoft 365 用户的默认 MFA。
如果您在单击保存按钮后收到错误通知,请检查身份验证方法策略并查看它是否在 Microsoft Entra ID 中启用。请参阅将旧版 MFA 和 SSPR 迁移到身份验证方法策略一文的第一部分。
无法保存默认登录方法:无法将语音通话(主要移动设备)保存为默认登录方法。 - 未为此用户启用身份验证方法策略。无法更新 UserPreferredMethodForSecondaryAuthentication。
如果需要更改多个 Microsoft 365 用户的身份验证方法以加快该过程,请在下一步中使用 PowerShell。
使用 PowerShell 更改默认 MFA 方法
假设某个组织决定为所有 Microsoft 365 用户使用不同的默认登录方法。因此,使用 PowerShell 脚本更加容易和快捷。
多重身份验证 (MFA) 方法
下表显示了您可以为用户设置的身份验证方法。您必须在后续步骤的 PowerShell 脚本中指定默认方法并使用正确的 MFA 缩写。
- Microsoft Authenticator notification on app
推
- OATH TOTP (6-digit) one-time code on a third-party software app
誓言
- SMS (6-digit) code as a text message on primary mobile
短信
- Voice call on the office phone
语音替代移动
- Voice call on primary mobile
语音移动
- Voice call on the office phone
-
语音办公室
连接到 Microsoft Graph PowerShell
在开始之前,必须安装 Microsoft Graph PowerShell 模块,包括 Microsoft Graph Beta 模块。
运行以下命令来安装 Microsoft Graph 模块。
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。
您还需要使用以下范围连接到 MS Graph。
Connect-MgGraph -Scopes "User.Read.All", "UserAuthenticationMethod.ReadWrite.All"更改单用户的默认 MFA 方法
要更改单个 Microsoft 365 用户的默认 MFA 方法,请使用以下 PowerShell 脚本:
- 在第 5 行中输入用户的 UserPrincipalName
- 在第 8 行中指定首选 MFA 方法
- 运行以下 PowerShell 脚本
# Connect to Microsoft Graph using the specified authentication scopes
Connect-MgGraph -Scopes "UserAuthenticationMethod.ReadWrite.All", "User.Read.All"
# Specify the user for whom you want to update the preferred method
$UserUPN = "Amanda.Hansen@m365info.com"
# Set the desired value for the preferred method
$PreferredMethod = "push"
# Get the user from Microsoft Graph
$User = Get-MgUser -UserId $UserUPN
# Create a JSON body template for the API request
$body = @{
userPreferredMethodForSecondaryAuthentication = $PreferredMethod
}
# Construct the API endpoint URL for getting the user's authentication sign-in preferences
$uri = "https://graph.microsoft.com/beta/users/$($User.Id)/authentication/signInPreferences"
# Send a GET request to the API endpoint to check the user's preferred method
$Check = Invoke-MgGraphRequest -uri $uri -Method GET -OutputType PSObject
# Check if the user already has the preferred method set
if ($Check.userPreferredMethodForSecondaryAuthentication -eq $PreferredMethod) {
Write-host "Default MFA method $PreferredMethod already set for $($User.UserPrincipalName)" -ForegroundColor Cyan
}
else {
try {
# Send a PATCH request to update the user's preferred method
Invoke-MgGraphRequest -uri $uri -Body $body -Method PATCH -ErrorAction Stop
Write-host "Default MFA method $PreferredMethod updated successfully for $($User.UserPrincipalName)" -ForegroundColor Green
}
catch {
# The registered method is not found for the user
Write-Host "Default MFA method $PreferredMethod is not registered by $($User.UserPrincipalName)" -ForegroundColor Yellow
}
}PowerShell 输出结果显示以下三种不同结果之一:
Default MFA method push already set for Amanda.Hansen@m365info.com
Default MFA method push updated successfully for Amanda.Hansen@m365info.com
Default MFA method push is not registered by Amanda.Hansen@m365info.com如果未注册MFA方法,则意味着用户需要先添加身份验证方法。
更改所有用户的默认 MFA 方法
要更新组织中所有用户的身份验证方法,我们需要使用 PowerShell 脚本。
在我们的示例中,我们希望将 Microsoft Authenticator 设置为所有用户的默认方法,因为它比发送短信或接听电话更安全。
运行下面的 PowerShell 脚本以更改所有用户的默认 MFA 方法:
- 在第 5 行中指定首选 MFA 方法
- 在第 6 行上指定 CSV 文件的路径
# Connect to Microsoft Graph using the specified authentication scopes
Connect-MgGraph -Scopes "UserAuthenticationMethod.ReadWrite.All", "User.Read.All"
# Set the desired value for the preferred method and the CSV file path
$PreferredMethod = "push"
$CsvPath = "C:\temp\MFAMethodReport.csv"
# Get all users from Microsoft Graph
$AllUsers = Get-MgUser -All
# Create a JSON body template for the API request
$body = @{
userPreferredMethodForSecondaryAuthentication = $PreferredMethod
}
# Create an empty array to store the results
$results = @()
# Loop through each user
foreach ($User in $AllUsers) {
# Construct the API endpoint URL for getting the user's authentication sign-in preferences
$uri = "https://graph.microsoft.com/beta/users/$($User.Id)/authentication/signInPreferences"
# Send a GET request to the API endpoint to check the user's preferred method
$Check = Invoke-MgGraphRequest -uri $uri -Method GET -OutputType PSObject
# Check if the user already has the preferred method set
if ($Check.userPreferredMethodForSecondaryAuthentication -eq $PreferredMethod) {
# Skip the user and add the result to the array
Write-host "Default MFA method $PreferredMethod already set for $($User.UserPrincipalName)" -ForegroundColor Cyan
$result = [PSCustomObject]@{
DisplayName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
Status = "Already set"
}
$results += $result
continue
}
try {
# Send a PATCH request to update the user's preferred method
Invoke-MgGraphRequest -uri $uri -Body $body -Method PATCH -ErrorAction Stop
$result = [PSCustomObject]@{
DisplayName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
Status = "Updated"
}
$results += $result
Write-host "Default MFA method $PreferredMethod updated successfully for $($User.UserPrincipalName)" -ForegroundColor Green
}
catch {
# The registered method is not found for the user
Write-Host "Default MFA method $PreferredMethod is not registered by $($User.UserPrincipalName)" -ForegroundColor Yellow
$result = [PSCustomObject]@{
DisplayName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
Status = "Method not registered"
}
$results += $result
}
}
# Export the results to a CSV file
$results | Export-Csv -Path $csvPath -Encoding utf8 -NoTypeInformation
Write-Host "Results saved to $($CsvPath)" -ForegroundColor Cyan最后,您将获得一个 CSV 文件报告,其中显示所有用户的状态,默认方法为:
- 已设置
- 已更新
- 未注册
CSV 文件显示更新 MFA 方法失败的用户。如果结果显示未注册,则表示用户尚未设置您指定的默认方法(Microsoft Authenticator)。下一步是向这些用户发送电子邮件,以便他们可以添加身份验证方法。几天后,您可以再次运行该脚本。
转到 C:\temp 并使用 Microsoft Excel 打开 MFAMethodReport.csv 文件。
就是这样。
了解更多:如何提高 Microsoft Entra MFA 安全性 »
结论
您了解了如何更改所有用户的默认 MFA 方法。在 Microsoft Entra 管理中心中,您只能更新单个用户的默认登录。使用 PowerShell 并更改所有用户的默认身份验证方法要快得多。当您更新所有用户的 MFA 方法时,它会将默认身份验证方法状态导出到 CSV 文件报告。它显示哪些用户已设置、更新或未注册 MFA 方法。
您喜欢这篇文章吗?您还可以在 Microsoft Entra 中配置条件访问策略。不要忘记关注我们并分享这篇文章。







