CSOM 登录:验证凭据时出错

问题描述 投票:0回答:1

美好的一天,

我正在使用 CSOM for .NET Standard 的现代身份验证。 我已遵循所描述的每一步:

  • 在 Entra 上注册了我的应用程序
  • 我有一个应用程序ID
  • 为应用程序提供 Sharepoint.AllSites.FullControl
  • 已获得管理员同意
  • 允许的公共客户端流量

当我运行代码时,它说: 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;
}

我错过了什么??

谢谢!

asp.net-mvc sharepoint csom
1个回答
0
投票

请检查该网站是否启用了 MFA。启用 MFA 的帐户无法在后台运行,并且需要交互式登录,因此如果您尝试执行此操作,则需要使用未启用 MFA 的服务帐户。 采取以下步骤禁用 MFA

https://answers.microsoft.com/en-us/msoffice/forum/all/how-can-we-disable-multi-factor-authentication-to/703fffbd-b8d4-4ffd-a8db-c053282265ff

© www.soinside.com 2019 - 2024. All rights reserved.