我目前正在尝试通过 C# 连接到我的 Microsoft SharePoint 网站。我正在使用 microsoft.sharepoint.client.online.csom .0.4919.1000 库,我没有成功...... 你能帮我吗?
我制作了一个小型控制台应用程序来测试,这是我测试的代码。
static async void CheckConnection()
{
var passTask = GetUserPasswordAsync();
string userID = <username>;
using (ClientContext ctx = new ClientContext("https://<company_sharepoint>.sharepoint.com/sites/<my_site>"))
{
await passTask;
if (passTask.Result is null)
return;
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userID, passTask.Result);
ctx.Credentials = credentials;
Web myWeb = ctx.Web;
ctx.Load(myWeb, w => w.Title);
ctx.ExecuteQuery();
await Console.Out.WriteLineAsync(myWeb.Title);
}
}
但是每当我运行这行代码时
ctx.ExecuteQuery();
它抛出错误
Microsoft.SharePoint.Client.IdcrlException: 'Identity Client Runtime Library (IDCRL) did not get a response from the Login server.'
我不知道到底发生了什么,也不知道我是否使用了正确的库或方法...... 有人可以帮助我吗?
可以参考以下代码
using Microsoft.SharePoint.Client;
using System;
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://your-sharepoint-site-url.sharepoint.com/sites/yoursite";
string username = "[email protected]";
string password = "your_password";
using (ClientContext ctx = new ClientContext(siteUrl))
{
// Online credentials
ctx.Credentials = new SharePointOnlineCredentials(username, ConvertToSecureString(password));
// Load the web
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
// Print some information about the web
Console.WriteLine($"Title: {web.Title}");
Console.WriteLine($"Description: {web.Description}");
Console.WriteLine($"Url: {web.Url}");
}
}
// Convert string to SecureString
private static System.Security.SecureString ConvertToSecureString(string password)
{
if (password == null)
throw new ArgumentNullException(nameof(password));
var securePassword = new System.Security.SecureString();
foreach (char c in password)
securePassword.AppendChar(c);
return securePassword;
}
}