【问题标题】:SharePoint Server Authentication ErrorSharePoint Server 身份验证错误
【发布时间】:2011-10-23 09:02:12
【问题描述】:

所以我之前使用的是托管的 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 网站上出现此错误?

【问题讨论】:

  • 请不要在标题中添加“C# -”之类的标签。你已经有了c# 标签,这样更好。
  • 我假设您已经单步执行了这段代码以查看哪一行实际上引发了异常。它是哪一个?我也没有看到“列表”变量的定义......
  • @John 对不起,我会注意这一点以供将来参考。
  • @meccaneko 抱歉,我没有包含所有代码。但是那个定义就在那里,我把它添加到了 OP 中。错误发生在第 130 行或clientContext.ExecuteQuery();,但这对我没有多大帮助。 ://

标签: c# sharepoint authentication sharepoint-clientobject


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    相关资源
    最近更新 更多