在asp.net中使用客户端ID和客户端密码访问Sharepoint列表C#
当前,我可以使用用户ID和密码访问SharePoint列表,如下所示。但想了解如何使用客户端ID和客户端密码访问列表?在asp.net中使用客户端ID和客户端密码访问Sharepoint列表C#
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();
回答:
您可以使用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();
}
要添加即插即用CSOM核心,去你的项目引用>管理的NuGet包。
添加SharePointPnPCoreOnline包。
参考 - Authenticate SharePoint using PnP Authentication Manager
Expose on public web your SharePoint Online information
以上是 在asp.net中使用客户端ID和客户端密码访问Sharepoint列表C# 的全部内容, 来源链接: utcz.com/qa/259861.html