代码之家  ›  专栏  ›  技术社区  ›  Kevin Pope

使用CSOM对Sharepoint Online进行身份验证

  •  0
  • Kevin Pope  · 技术社区  · 7 年前

    我觉得我错过了什么。我正试图通过C#控制台应用程序登录到SPO租户,但出现错误:

    无法联系网站' https://xxx.sharepoint.com/ '或网站不支持SharePoint Online凭据。响应状态代码为“未授权”。

    我知道这个帐户可以工作,因为我可以直接从浏览器用这个帐户登录。最后,我会 使用CSOM对SP列表、术语库和文档库执行CRUD操作,但CSOM不是一个硬性要求(我承认不知道restapi是否可以完成全部工作)。

    我看到这改变了世界 LegacyAuthProtocolsEnabled 值为True可以帮助实现这一点,但我们的安全人员不允许我启用该功能。

    SecureString passWord = getPassword();
    using (var context = new ClientContext(URI))
    {
        context.Credentials = new SharePointOnlineCredentials(userName, passWord);//new NetworkCredential(userName, passWord);//
        context.Load(context.Web, web => web.Title);
        context.ExecuteQuery(); //Error happens here
        Console.WriteLine("Your site title is: " + context.Web.Title);
    }
    

    如你所见,我也试过通过 NetworkCredentials 对象,这也不起作用(我也得到了401响应)。

    this page )

    基于此,你能看出我做错了什么吗?或者如果有其他更好的方法,我也愿意接受!

    :看起来像是 如果设置为False,则显式不能使用 SharePointOnlineCredentials this page . 鉴于此,看来我需要一个不同的方法来获得这个访问!

    3 回复  |  直到 7 年前
        1
  •  1
  •   0xced    6 年前

    您描述的守护程序场景可以通过使用Azure应用程序通过JWT令牌进行身份验证和获取权限来实现。有关漫游,请参见 Authenticating to Azure AD non-interactively using a username & password or Windows Integrated Authentication

    如果你之前没有使用过azure应用程序,我建议你花点时间熟悉一下。它们基本上是您创建和批准的受信任主体,以便您的应用程序使用其权限执行SharePoint操作。

    我也不再为任何新的开发推荐CSOM,特别是SP Online,因为我相信微软(或多或少公开地)将退出这个API。使用Gautam推荐的PnP库,这是最新的,是restapi和托管Metadate等的一个很好的包装器。

        2
  •  8
  •   Gautam Sheth    7 年前

    LegacyAuthProtocolsEnabled 设置为 False

    为了向SPO进行身份验证,我们使用 GetWebLoginClientContext SharePoint PnP核心库的方法,该库作为nuget包提供。

    AuthenticationManager authManager = new AuthenticationManager();
    using (var context = authManager.GetWebLoginClientContext(URI))
    {
        context.Load(context.Web, web => web.Title);
        context.ExecuteQuery();
        Console.WriteLine("Your site title is: " + context.Web.Title);
    }
    

    添加 SharePointPnPCoreOnline

        3
  •  -3
  •   TylerH Ash Burlaczenko    5 年前
    string siteUrl="your Site Url";
        string username="UserName";
        string password="Password";
    
        public static ClientContext CreateClientContext(string siteUrl,string username,string password)
        {
            ClientContext context = new ClientContext(siteUrl);
            var securePassword = new SecureString();
            foreach (var chr in password) securePassword.AppendChar(chr);
            context.Credentials = new SharePointOnlineCredentials(username, securePassword);
            return context;
        }
    
    推荐文章