美好的一天,
我正在使用 CSOM for .NET Standard 的现代身份验证。 我已遵循所描述的每一步:
当我运行代码时,它说: var token = Object : "{"error":"invalid_grant","error_description":"AADSTS50126: 由于用户名或密码无效,验证凭据时出错。 跟踪 ID:
我 100% 确定用户名和密码正确。
Uri SPURL = new Uri(settings.SP_URL);
string SPUserName = settings.SP_UserName;
string SPPassword = Tools.DecryptText(settings.SP_Pass);
SecureString secureSPPassword = new SecureString();
foreach (char c in SPPassword) secureSPPassword.AppendChar(c);
// Note: The PnP Sites Core AuthenticationManager class also supports this
using (var authenticationManager = new AuthenticationManager())
using (var context = authenticationManager.GetContext(SPURL, SPUserName, secureSPPassword))
{
context.Load(context.Web, p => p.Title);
await context.ExecuteQueryAsync();
Console.WriteLine($"Title: {context.Web.Title}");
return context.Web.Title;
}
var clientId = defaultAADAppId;
var body = $"resource={resource}&client_id={clientId}&grant_type=password&username={HttpUtility.UrlEncode(username)}&password={HttpUtility.UrlEncode(password)}";
using (var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"))
{
var result = await httpClient.PostAsync(tokenEndpoint, stringContent).ContinueWith((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
}).ConfigureAwait(false);
var tokenResult = JsonSerializer.Deserialize<JsonElement>(result);
var token = tokenResult.GetProperty("access_token").GetString();
return token;
}
我错过了什么??
谢谢!
请检查该网站是否启用了 MFA。启用 MFA 的帐户无法在后台运行,并且需要交互式登录,因此如果您尝试执行此操作,则需要使用未启用 MFA 的服务帐户。 采取以下步骤禁用 MFA