pnpframework icon indicating copy to clipboard operation
pnpframework copied to clipboard

The SSL connection could not be established - The remote certificate is invalid because of errors in the certificate chain: PartialChain

Open Misiu opened this issue 11 months ago • 1 comments

I'm trying to migrate my custom code to PnP.Framework, but sadly I have problems at the beginning. I'm using SharePoint Server 2019 on-premise, which is available via IP address, I used etc/.hosts to assign a domain name to it.

my site is available at: https://shrserver2019te.dev.local/sites/clients/DEV-0000005094

below is my code:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

Console.WriteLine("PNP!");

// Connect to SharePoint on-premise using PNP
string siteUrl = "https://shrserver2019te.dev.local/sites/clients/DEV-0000005094";
string userName = "dev";
string password = "xxxpassxxx";

var networkCredential = new NetworkCredential(userName, password);
var authManager = new AuthenticationManager();

using (var clientContext = authManager.GetOnPremisesContext(siteUrl, networkCredential))
{

    clientContext.ExecutingWebRequest += (sender, e) =>
    {
        e.WebRequestExecutor.WebRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
    };

    clientContext.Load(clientContext.Web, web => web.Title);
    await clientContext.ExecuteQueryAsync();

    Console.WriteLine(clientContext.Web.Title);
}

Sadly, every time I try to make a simple request, I get this error:

System.Net.Http.HttpRequestException
  HResult=0x80131501
  Message=The SSL connection could not be established, see inner exception.
  Source=System.Net.Http
  StackTrace:
   at System.Net.Http.ConnectHelper.<EstablishSslConnectionAsync>d__2.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs:line 99
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
   at System.Net.Http.HttpConnectionPool.<ConnectAsync>d__103.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 1580
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 816
   at System.Net.Http.HttpConnectionPool.<CreateHttp11ConnectionAsync>d__105.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 1661
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
   at System.Net.Http.HttpConnectionPool.<AddHttp11ConnectionAsync>d__79.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 499
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.<WaitWithCancellationAsync>d__1.MoveNext() in /_/src/libraries/Common/src/System/Threading/Tasks/TaskCompletionSourceWithCancellation.cs:line 21
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
   at System.Net.Http.HttpConnectionPool.HttpConnectionWaiter`1.<WaitForConnectionWithTelemetryAsync>d__6.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 2632
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
   at System.Net.Http.HttpConnectionPool.<SendWithVersionDetectionAndRetryAsync>d__89.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 1112
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
   at System.Net.Http.AuthenticationHelper.<SendWithAuthAsync>d__17.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.cs:line 240
   at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
   at System.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs:line 30
   at System.Net.Http.HttpClient.<<SendAsync>g__Core|83_0>d.MoveNext() in /_/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs:line 530
   at Microsoft.SharePoint.Client.ClientContextExtensions.<GetOnPremisesRequestDigestInfoAsync>d__25.MoveNext() in /_/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs:line 996
   at Microsoft.SharePoint.Client.ClientContextExtensions.<GetOnPremisesRequestDigestAsync>d__24.MoveNext() in /_/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs:line 961
   at PnP.Framework.AuthenticationManager.<>c.<ConfigureOnPremisesContext>b__77_0(Object sender, WebRequestEventArgs webRequestEventArgs) in /_/src/lib/PnP.Framework/AuthenticationManager.cs:line 1367
   at Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)
   at Microsoft.SharePoint.Client.ClientRuntimeContext.FireExecutingWebRequestEvent(WebRequestEventArgs args)
   at Microsoft.SharePoint.Client.ClientRequest.SetupServerQuery(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientRequest.<ExecuteQueryToServerAsync>d__53.MoveNext()
   at Microsoft.SharePoint.Client.ClientRequest.<ExecuteQueryAsync>d__39.MoveNext()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.<ExecuteQueryAsync>d__57.MoveNext()
   at Microsoft.SharePoint.Client.ClientContext.<ExecuteQueryAsync>d__23.MoveNext()
   at SharepointPnP.Program.<Main>d__0.MoveNext() in C:\Users\Misiu\source\repos\SPTest\Sharepoint.PnP\Program.cs:line 34
   at SharepointPnP.Program.<Main>(String[] args)

  This exception was originally thrown at this call stack:
    System.Net.Security.SslStream.CompleteHandshake(System.Net.Security.SslAuthenticationOptions) in SslStream.IO.cs
    System.Net.Security.SslStream.ForceAuthenticationAsync<TIOAdapter>(bool, byte[], System.Threading.CancellationToken) in SslStream.IO.cs
    System.Net.Security.SslStream.ProcessAuthenticationWithTelemetryAsync(bool, System.Threading.CancellationToken) in SslStream.IO.cs
    System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(System.Net.Security.SslClientAuthenticationOptions, System.Net.Http.HttpRequestMessage, bool, System.IO.Stream, System.Threading.CancellationToken) in ConnectHelper.cs

Inner Exception 1:
AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: PartialChain

The question is: How can I disable certificate validation for all requests done by PnP.Framework?

Misiu avatar Feb 05 '25 07:02 Misiu

I'm able to do basic REST requests using simple HttpClient with a custom HttpClientHandler

internal class SharepointHttpClientHandler : HttpClientHandler
{
    public SharepointHttpClientHandler()
    {
        Credentials = new NetworkCredential("DEV", "SecurePass");
        ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
    }
}

with this client, I'm able to get lists, and folders and create items. Can a similar thing be done with PnP.Framework?

Misiu avatar Feb 18 '25 09:02 Misiu