使用 AzureKeyVault 时如何提示 MFA?

How can I prompt for MFA when using AzureKeyVault?

我使用存储在 Azure Key Vault 中的机密管理桌面应用程序。安全组被授予访问 Azure Key Vault 的权限,并且该安全组中的用户应该能够访问密钥。我使用以下代码访问 Azure Key Vault 并检索机密:

var tokenCallback = new AzureServiceTokenProvider("RunAs=CurrentUser").KeyVaultTokenCallback;
var authCallback = new KeyVaultClient.AuthenticationCallback(tokenCallback);
var client = new KeyVaultClient(authCallback);
var secret = await client.GetSecretAsync("https://keyvaultnameplaceholder.vault.azure.net/", SecretName);

我的大多数用户都成功了。最近,部分用户的代码开始出现错误:

Exception Message: Tried to get token using Active Directory Integrated Authentication. Access token could not be acquired. 
AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access

我的理解是Active Directory集成身份验证不支持MFA;但是,我一直无法弄清楚如何强制 Azure Key Vault 执行 MFA 提示。我使用 Active Directory 交互式身份验证(支持 MFA)为其他地方的应用程序获取 AAD 令牌,但那里不需要 MFA 提示。我已将该令牌传递给 KeyVaultClient,但失败了,因为不允许应用程序本身访问 AzureKeyVault:只有用户自己。

有什么方法可以让 Azure Key Vault 提示 MFA/force Azure Key Vault 以使用 AAD 通用或交互式登录?

Active Directory Integrated Authentication没有绕过MFA,如果配置了MFA,可能会失败。

在你的情况下,如果你想要交互式登录,我的解决方法是 use the Azure CLI to authenticate,它支持 MFA 交互式登录。

1.Install Azure CLI,使用az login登录您的用户帐户。

2.Then 如下更改代码。

var tokenCallback = new AzureServiceTokenProvider("RunAs=Developer; DeveloperTool=AzureCli").KeyVaultTokenCallback;