使用ASP.NET C#中的客户端ID和客户端密钥访问Sharepoint列表

AKR*_*AKR 0 c# asp.net sharepoint

目前,我可以使用用户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();
Run Code Online (Sandbox Code Playgroud)

Gau*_*eth 5

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

之后,您可以使用以下代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

添加SharePointPnPCoreOnline包。

在此处输入图片说明

参考- 使用PnP身份验证管理器对SharePoint进行身份验证

在公共网站上公开您的SharePoint Online信息