Search code examples
asp.netsharepointcsom

ASP.NET Sharepoint CSOM error: An existing connection was forcibly closed by the remote host


I have an ASP.NET application running under .NET Framework 4.7.2. In my application I am trying to connect to my Sharepoint site to create or rename folders. Unfortunately I am getting below error. I've been searching around on the internet and found a lot of articles talking about TLS but my application has TLS 1.2 by default due to the .NET Framework 4.7.2.

Does anyone would know the reason/background of the error? (I don't have the error on my local machine (no SSL), error only occurs after deploying to my hosting environment with SSL)

This is the full error message:

Server Error in '/' Application.
An existing connection was forcibly closed by the remote host
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
   System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +106
   System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +130

[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.]
   System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +291
   System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) +32
   System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) +156
   System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) +281
   System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) +49
   System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) +162
   System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) +542
   System.Net.TlsStream.CallProcessAuthentication(Object state) +42
   System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) +228
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) +21
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) +64
   System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) +778
   System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) +52
   System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) +103
   System.Net.ConnectStream.WriteHeaders(Boolean async) +388

[WebException: The underlying connection was closed: An unexpected error occurred on a send.]
   System.Net.HttpWebRequest.GetResponse() +1399
   Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() +37
   Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate() +514
   Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() +126
   Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() +44
   Axius.Presentation.Website.frmAdminProjectDossierDetail.chkDossierSharepoint_CheckedChanged(Object sender, EventArgs e) in C:\inetpub\wwwroot\Axius.Presentation.Website\frmAdminProjectDossierDetail.aspx.cs:3637
   System.Web.UI.WebControls.CheckBox.OnCheckedChanged(EventArgs e) +116
   System.Web.UI.WebControls.CheckBox.RaisePostDataChangedEvent() +139
   System.Web.UI.WebControls.CheckBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +12
   System.Web.UI.Page.RaiseChangedEvents() +132
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1559

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3930.0

And this is the code:

using (ClientContext context = new ClientContext("https://mysite.sharepoint.com/"))
{
    var passWord = new SecureString();

    foreach (char c in "XXXXXXXXXX".ToCharArray()) passWord.AppendChar(c);

    context.Credentials = new SharePointOnlineCredentials("my@account.com", passWord);
                    
    List oList = context.Web.Lists.GetByTitle(@"Gedeelde documenten");

    FolderCollection folders = oList.RootFolder.Folders;
    context.Load(folders);                    
    context.ExecuteQuery();     //this is frmAdminProjectDossierDetail.aspx.cs:3637 visible in the error              

    foreach (Folder folderr in folders)
    {
        if (folderr.Name == "01 PROJECTEN DOCS")
        {
            //code continues
        }
    }                    
}

Solution

  • There are at least two parts of a TLS connection: version and ciphersuites. You need to check the ciphers you have on your local machine:

    https://learn.microsoft.com/en-us/powershell/module/tls/get-tlsciphersuite?view=windowsserver2022-ps

    Then you should check the ciphers that are available on the sharepoint site: https://www.ssllabs.com/ssltest/

    At least one of the ciphers must match in order to complete the connection.