我正在尝试使用.NET ADAL库验证Azure AD中的用户密码.
这适用于没有MFA的常规用户帐户,但是对于已激活MFA的用户,我遇到了问题.
当使用用户的实际密码时,我得到了AADSTS50076:需要应用程序密码.这是公平的,但是当我创建新的应用程序密码时,我收到错误AADSTS70002:验证凭据时出错. AADSTS50020:用户名或密码无效.我创建了多个应用密码,但它们都不起作用.
用于尝试身份验证的代码如下:
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]", "my-password"));
是否可以为具有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的用户必须使用其实际帐户密码才能登录.似乎不支持使用应用密码.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。