Microsoft.SharePoint.Client.IdcrlException: 'The sign-in name or password---' 即使使用 Pnp 身份验证管理器

Microsoft.SharePoint.Client.IdcrlException: 'The sign-in name or password---' Even WITH Pnp Authentication Manager

我已经通过 Microsoft 365 E5 Developer(没有 Windows 和音频会议) 注册了 Office 365 Developer Edition。我正在编写代码以连接到开发人员域的 Sharepoint。以下是我的代码:

   public static String GetList( ICredentials credentials)
    {
        var authManager = new OfficeDevPnP.Core.AuthenticationManager();
        using (ClientContext clientContext = 
        authManager.GetWebLoginClientContext("https://xxx.sharepoint.com"))
        {
            clientContext.Credentials = credentials;

            Web web = clientContext.Web;
            clientContext.Load(web,
            webSite => webSite.Title);

            clientContext.ExecuteQuery();
            return web.Title;


        }
    }


    public string callSharepoint()
    {


        const string userName = "Username@domain.onmicrosoft.com";  
        const string password = "xxxx";
        var securePassword = new SecureString();
        foreach (var c in password)
        {
            securePassword.AppendChar(c);
        }
        var credentials = new SharePointOnlineCredentials(userName, securePassword);

        var list = GetList(credentials); 
        return list.ToString();
    }

而运行,它首先要求输入Microsoft Office凭据,然后通过将代码发送到联系电话进行验证,然后在验证完成后在线抛出异常 clientContext.ExecuteQuery()。异常如下:

Microsoft.SharePoint.Client.IdcrlException: 'The sign-in name or password does not match one in the Microsoft account system.'

我使用的凭据是具有 角色 全局管理员 的管理员帐户。我还尝试在该 Active Directory 中添加新用户帐户并尝试使用该凭据,但仍然在同一个地方遇到相同的异常。 我什至尝试删除 Pnp 授权、启用和禁用多因素授权,但没有成功。但是,我可以使用完全相同的凭据在浏览器上成功登录到 Sharepoint 站点。

我认为,我在设置 Office 帐户开发者订阅时所做的设置很可能存在问题。也许代码没有问题,因为我使用相同的代码登录到我组织的 Sharepoint 并且它工作得很好。也许我需要在开发人员的 Office 帐户中配置其他内容。

如果有人对此问题有所了解,请告诉我。

您已初始化凭据,可以直接使用,如果存在问题,应该与您的用户帐户或许可证有关。

public static String GetList(ICredentials credentials)
        {
            //var authManager = new OfficeDevPnP.Core.AuthenticationManager();
            //using (ClientContext clientContext =
            //authManager.GetWebLoginClientContext("https://xxx.sharepoint.com/sites/lee"))
            //{

            //}

            using (ClientContext clientContext = new ClientContext("https://xxx.sharepoint.com/sites/lee"))
            {
                clientContext.Credentials = credentials;

                Web web = clientContext.Web;
                clientContext.Load(web,
                webSite => webSite.Title);

                clientContext.ExecuteQuery();
                return web.Title;
            }
        }


        public string callSharepoint()
        {


            const string userName = "user@xxx.onmicrosoft.com";
            const string password = "password";
            var securePassword = new SecureString();
            foreach (var c in password)
            {
                securePassword.AppendChar(c);
            }
            var credentials = new SharePointOnlineCredentials(userName, securePassword);

            var list = GetList(credentials);
            return list.ToString();
        }

好的,我找到了解决方案。 那就是我不需要这行代码:

     clientContext.Credentials = credentials;

由于启用了 MFA,所以当我通过 Pnp Authenticator 登录时,它应该使用该用户帐户。而不是通过 SharePointOnlineCredentials 传递的那个。