0

因此,我之前使用过托管的 SharePoint 网站,当我使用以下代码对该网站进行身份验证时,一切正常:

private static NetworkCredential credentials = new NetworkCredential(username, password, domain);
private static ClientContext clientContext = new ClientContext("https://thisonesite.com/hosting/151");
private static Web site = clientContext.Web;
private static List list = site.Lists.GetByTitle(listName);

private static void sharepointLogin()
{
    try
    {
       //Loads credentials needed for authentication
       clientContext.Credentials = credentials;
       //Loads the site
       clientContext.Load(site);
       //Loads the site list
       clientContext.Load(list);
       //Executes the previous queries on the server
       clientContext.ExecuteQuery();
       //Writes out the title of the SharePoint site to the console
       Console.WriteLine("Title: {0}", site.Title);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

现在,我为我创建了一个沙箱,我可以(手动)访问该站点。但是,当我尝试通过我的 c# 代码对站点进行身份验证时,出现错误。

除了用户名、密码和域之外,我真正改变的只是private static ClientContext clientContext = new ClientContext("http://sandbox");.

我在控制台中得到的错误是:

System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at ConsoleApp1.SharePointAccess.sharepointLogin() in C:\directortypath

有谁知道为什么在新的 SharePoint 网站上突然出现此错误?

4

1 回答 1

0

在每个 executeQuery() 之前都需要一个凭证

并且您需要两个 executeQuery() 来分别加载站点和列表。

clientContext.Credentials = credentials;
       clientContext.Load(site);
       clientContext.ExecuteQuery();

clientContext.Credentials = credentials;
       clientContext.Load(list);
       clientContext.ExecuteQuery();
于 2014-06-27T08:34:17.280 回答