【问题标题】:Authenticate to SharePoint Online C#向 SharePoint Online C# 进行身份验证
【发布时间】:2021-11-18 11:52:22
【问题描述】:

我正在尝试在控制台应用程序中在线连接到 SharePoint 并打印网站的标题。 它给了我错误:“登录名或密码与 Microsoft 帐户系统中的一个不匹配。” 我已检查并确保用户名和密码 100% 正确。 我不知道还要检查什么 这是我的代码:

private static void SPCredentialsConnect()
    {
        const string SiteUrl = "https://tenant.sharepoint.com/sites/mysite";
        const string pwd = "appPassword";
        const string username = "username@tenant.onmicrosoft.com";

        SecureString securestring = new SecureString();
        pwd.ToCharArray().ToList().ForEach(s => securestring.AppendChar(s));

        ClientContext context = new ClientContext(SiteUrl);
        context.Credentials = new SharePointOnlineCredentials(username, securestring);

        try
        {
            var web = context.Web;
            context.Load(web);
            context.ExecuteQuery();

            Console.WriteLine($"web title: {web.Title}");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

【问题讨论】:

  • 更新:我已经尝试过 AppOnly 身份验证,但也没有用: public static void AppOnlyAuthCall() { using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret)) { clientContext .Load(clientContext.Web, page => page.Title); clientContext.ExecuteQuery(); Console.WriteLine(clientContext.Web.Title); }; }
  • 当我使用此代码时,它告诉我“System.Net.WebException:'远程服务器返回错误:(401)未经授权。”是的,在 Azure Active Directory 中,我已授予 API 权限(委托 allsites 完全控制),“allowPublicClient”:true,“oauth2AllowUrlPathMatching”:true。

标签: c# authentication sharepoint console-application


【解决方案1】:

您的问题解决了吗? “登录名或密码与微软帐户系统中的一个不匹配”有时会出现错误,一段时间后修复,没有任何变化。

无法在 Azure Active Directory 中注册 sharepointonline 的 AppOnly 身份验证。 应该注册在

https://contoso.sharepoint.com/_layouts/15/appregnew.aspx

并授予权限

https://contoso-admin.sharepoint.com/_layouts/15/appinv.aspx

您可以参考以下文档

https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

【讨论】:

  • 我确实注册了它并授予了权限 - 信任 - 使用了 AppConfig,但仍然给我一个错误 401 denied with app only auth。当通过 SP Credentials 连接时,同样的错误没有修复(登录名或密码不正确)
  • 您的帐户可能已启用 MFA(多因素身份验证)!如果是,则必须关闭 MFA,或者您必须按照如何从 PowerShell 使用启用 MFA 的帐户连接到 SharePoint Online 中描述的方法
  • MFA 被禁用(一开始没有设置 - 默认情况下也没有设置)这太奇怪了两个错误仍然相同 - 使用凭据我得到“登录用户名或密码不正确” - 使用身份验证管理器我得到“远程服务器禁止 401”
猜你喜欢
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
相关资源
最近更新 更多