使用ASP.NET C#中的客户端ID和客户端密钥访问Sharepoint列表 [英] Access Sharepoint list using client Id and Client secret in asp.net C#

查看:74
本文介绍了使用ASP.NET C#中的客户端ID和客户端密钥访问Sharepoint列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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进行身份验证

公开曝光将您的SharePoint Online信息网络化

这篇关于使用ASP.NET C#中的客户端ID和客户端密钥访问Sharepoint列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆