【问题标题】:Access Sharepoint list using client Id and Client secret in asp.net C#在asp.net C#中使用客户端ID和客户端密码访问Sharepoint列表
【发布时间】:2021-10-13 14:17:30
【问题描述】:

目前,我可以使用下面给出的用户 ID 和密码访问共享点列表。但想了解如何使用客户端 ID 和客户端密码访问列表?

string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
ClientContext clientContext = new ClientContext(siteUrl);
string username = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
System.Security.SecureString passWord = new System.Security.SecureString();
foreach (char c in password.ToCharArray()) 
{    
    passWord.AppendChar(c);
}

clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();

【问题讨论】:

    标签: c# asp.net sharepoint


    【解决方案1】:

    您可以使用 PnP CSOM 内核的GetAppOnlyAuthenticatedContext 方法。

    之后就可以使用下面的代码了:

    string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
    string clientId = "<client-id>";
    string clientSecret = "<client-secret>";
    
    using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,clientId,clientSecret))
    {       
        Web oWebsite = clientContext.Web;
        ListCollection collList = oWebsite.Lists;
        clientContext.Load(collList);
        clientContext.ExecuteQuery();
    }
    

    要添加 PnP CSOM 核心,请转到您的项目参考 > 管理 nuget 包。

    添加 SharePointPnPCoreOnline 包。

    参考资料 - Authenticate SharePoint using PnP Authentication Manager

    Expose on public web your SharePoint Online information

    【讨论】:

    • 我正在使用相同的代码,但出现以下错误:System.TypeInitializationException:'OfficeDevPnP.Core.Utilities.TokenHelper' 的类型初始化程序引发了异常。 ---> System.TypeLoadException:无法从程序集“System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Web.Configuration.WebConfigurationManager”。在 OfficeDevPnP.Core.Utilities.TokenHelper..cctor() 请帮助我。
    • FWIW,SharePointPnPCoreOnline 包似乎已退役;使用PnP.Framework是新版本,GetACSAppOnlyContext替换GetAppOnlyAuthenticatedContext。
    【解决方案2】:

    SharePointPnPCoreOnline已经退役,如果您的项目目标框架是.Net Core,安装它会导致库冲突问题。

    这是经过测试并适用于 .Net Core 的代码。

    后继 Nuget 包名称:PnP.Framework

    命名空间:

    using PnP.Framework;  
    using Microsoft.SharePoint.Client;
    

    示例代码:

        string siteUrl = "https://xyz.sharepoint.com/sites/MySubSite" ;
        string ClientSecret = "<client-secret>";
        string ClientId = "<client-id>";
    
    
        using (var clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, ClientId, ClientSecret))
        {
            Web oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;
            clientContext.Load(collList);
            clientContext.ExecuteQuery();
            string result = "Total SP Library count is " + collList.Count();
    
        }
    

    【讨论】:

    • 我收到“远程服务器返回错误:(403) Forbidden。”在做clientContext.ExecuteQuery()的时候。有什么想法吗?
    • 显然 SPO 正在抱怨访问,请确保客户端 ID 对您尝试获取数据的网站集\Site\List 有足够的访问权限。我不确定如何设置客户端 ID,因为这是我为我设置的 IT 设置,但本文将告诉您如何设置具有完全控制访问权限的客户端 ID 的相关信息,docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/…
    • 能否通过Client Id、Secret获取token并使用?
    • 我收到“不知道这样的主机。不知道这样的主机。”在 ExecuteQuery() 之后。我确定我提供了正确的 Url 和 ClientId & Secert。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多