简体   繁体   English

认证到 SharePoint 在线 C#

[英]Authenticate to SharePoint Online C#

I'm trying to connect to SharePoint online in a console App and print the title of the site.我正在尝试在控制台应用程序中在线连接到 SharePoint 并打印该站点的标题。 Its giving me the error: "The sign-in name or password does not match one in the Microsoft account system."它给了我错误:“登录名或密码与 Microsoft 帐户系统中的一个不匹配。” I have checked and made sure the username and password are 100% right.我已检查并确保用户名和密码 100% 正确。 I dont know what else to check Heres my code:我不知道还有什么要检查的继承我的代码:

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);
        }
    }

Have your issue fixed?你的问题解决了吗? “The sign-in name or password does not match one in the Microsoft account system” Error will occur sometimes and fixed after a while with nothing changed. “登录名或密码与Microsoft帐户系统中的一个不匹配”有时会出现错误,并在一段时间后修复,没有任何变化。

AppOnly Authentication for sharepointonline can't be registed in Azure Active Directory.无法在 Azure Active Directory 中注册 sharepointonline 的 AppOnly 身份验证。 It should be register in应该注册在

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

And grant permission in并授予权限

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

You can refer to following document您可以参考以下文档

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

Consider using the PnP.Framework ( a NuGet package ), and use the AuthenticationManager object for SPO sites.考虑使用 PnP.Framework ( a NuGet package ),并为 SPO 站点使用 AuthenticationManager object。 This method bypasses MFA (which is mandatory in our organization, FWIW).此方法绕过 MFA(这在我们的组织 FWIW 中是强制性的)。 You can find a lot more information and examples here , including steps on getting the client id and client secret for a site.您可以在此处找到更多信息和示例,包括获取站点的客户端 ID 和客户端密码的步骤。 Here is what we use to log into SPO sites:以下是我们用来登录 SPO 网站的内容:

using (ClientContext context = 
    new AuthenticationManager().GetACSAppOnlyContext(SiteUrl, clientID, clientSecret)) 
    {
        ...
    }

Also, once you connect, you should adjust the Context.Load to grab the title if you want to use that value right away.此外,一旦连接,您应该调整 Context.Load 以获取标题,如果您想立即使用该值。 Here's what I used in my code:这是我在代码中使用的:

context.Load(web, p => p.Id, p => p.Title);
context.ExecuteQuery();
Console.WriteLine($"Logged into source {web.Title} ({web.Id})");

Good luck!祝你好运!

Steve in Spain史蒂夫在西班牙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM