本文介绍了在 asp.net C# 中使用客户端 ID 和客户端密码访问共享点列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我可以使用用户 ID 和密码访问共享点列表,如下所示.但是想了解如何使用 Client Id 和 Client secret 访问列表?
Currently, I'm able to access sharepoint list using user id and password as given below. But would like to understand on how can i access the list using Client Id and Client secret ?
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
方法.
You can use the GetAppOnlyAuthenticatedContext
method of PnP CSOM core.
之后,您可以使用以下代码:
After that you can use the code as below:
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 包.
To add PnP CSOM core, go to your project references > manage nuget packages.
添加 SharePointPnPCoreOnline 包.
Add the SharePointPnPCoreOnline package.
参考 - 使用 PnP 身份验证管理器验证 SharePoint
这篇关于在 asp.net C# 中使用客户端 ID 和客户端密码访问共享点列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!