如何在 .net CORE 3.1 中使用 ClientID 和机密对 SharePoint Online 进行身份验证

时间:2021-01-15 15:09:30

标签: asp.net-core authentication sharepoint-online

我尝试使用 .net CORE 控制台应用程序连接到 SharePoint 列表。我收到错误消息:

System.TypeInitializationException: 'The type initializer for 'OfficeDevPnP.Core.Utilities.TokenHelper' threw an exception.'
TypeLoadException: Could not load type 'System.Web.Configuration.WebConfigurationManager' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

以下是代码示例。在 .net 标准版本中同样可以正常工作:

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;

namespace Share_Point_POC_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
           
            string siteUrl = "https://a.b.com/sites/test";
            string clientId = "33345cb6-555a-777f-9b7b-0d3aaec2222c";
            string clientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx=";

            using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))
            {
                Web oWebsite = clientContext.Web;
                ListCollection collList = oWebsite.Lists;
                clientContext.Load(collList);
                clientContext.ExecuteQuery();
            }
        }
    }
}

.net core 中还有其他选择吗?

0 个答案:

没有答案