我正在寻找一个没有交互式登录的解决方案,但是到目前为止,我发现的所有解决方案都具有它。有人可以指出如何在不使用交互式方法的情况下获取启用了MFA的帐户的访问令牌吗?
这是我的代码:
string clientId = "CLIENTID";
string tenantID = "TENANTID";
string authority = $"https://login.microsoftonline.com/{tenantID}";
string[] scopes = new string[] { "https://outlook.office.com/EWS.AccessAsUser.All" };
string username ="USERNAME";
string password = "PASSWORD";
Microsoft.Identity.Client.IPublicClientApplication app;
app = Microsoft.Identity.Client.PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.Build();
var securePassword = new SecureString();
Dictionary<string, string> mfa = new Dictionary<string, string>();
mfa.Add("amr_values", "mfa");
foreach (char c in password) // you should fetch the password keystroke
securePassword.AppendChar(c); // by keystroke
var result = app.AcquireTokenByUsernamePassword(scopes,
username, securePassword)
.WithExtraQueryParameters(mfa)
.ExecuteAsync().Result;
我收到此错误:
MsalUiRequiredException:AADSTS50076:由于管理员所做的配置更改,或者由于您移至新位置,必须使用多重身份验证来访问'00000002-0000-0ff1-ce00-000000000000'。
这是Azure AD上的日志:
用户未通过MFA挑战(非交互式)。
如果为用户启用了MFA,则无法直接通过OAuth 2.0 Resource Owner Password Credentials获取令牌。
但是,有一个可选的解决方法。您可以通过以下方式获得带有刷新令牌的新令牌:Refresh the access token
这样,您无需任何交互即可获取新令牌。我看到您愿意在应用程序中使用密码,因此存储和检索刷新对您来说应该没问题。
注意:由于刷新令牌可用于获取新令牌,因此您需要将刷新令牌保存在安全的地方。