我正在尝试使用 .NET ADAL 库验证 Azure AD 中的用户密码。
这对于没有 MFA 的常规用户帐户来说效果很好,但对于激活了 MFA 的用户执行此操作时遇到了问题。
当使用用户的实际密码时,我得到了AADSTS50076: Application password is required.
,这很公平,但是当我创建新的应用程序密码时,我收到了错误AADSTS70002: Error validating credentials. AADSTS50020: Invalid username or password
。我创建了多个应用程序密码,但它们都不起作用。
用于尝试身份验证的代码如下:
var ac = new AuthenticationContext("https://login.windows.net/my-tenant.com");
var authResult = ac.AcquireToken("https://graph.windows.net", "my-client-id", new UserCredential("[email protected] /cdn-cgi/l/email-protection", "my-password"));
尝试进行身份验证的用户是此 AD 中的全局管理员。
是否可以对具有 MFA 的用户进行这样的身份验证?
因此,为了回答我自己的问题,我采取了以下措施(为了简洁而进行了清理):
public class AzureAdAuthenticationProvider
{
private const string AppPasswordRequiredErrorCode = "50076";
private const string AuthorityFormatString = "https://login.windows.net/{0}";
private const string GraphResource = "https://graph.windows.net";
private AuthenticationContext _authContext;
private string _clientId;
public AzureAdAuthenticationProvider()
{
var tenantId = "..."; // Get from configuration
_authContext = new AuthenticationContext(string.Format(AuthorityFormatString, tenantId));
}
public bool Authenticate(string user, string pass)
{
try
{
_authContext.AcquireToken(GraphResource, _clientId, new UserCredential(user, pass));
return true;
}
catch (AdalServiceException ase)
{
return ase.ServiceErrorCodes.All(sec => sec == AppPasswordRequiredErrorCode);
}
catch (Exception)
{
return false; // Probably needs proper handling
}
}
}
它并不漂亮,但它可以完成工作。
通过使用ServiceErrorCodes.All()
,我确保只有当发生单个 AppPasswordRequired 错误时,身份验证才成功。
此方法的唯一缺点是启用 MFA 的用户必须使用其实际帐户密码才能登录。似乎不支持使用应用程序密码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)