Ignore SSL certificate in .Net Standard 2.0

5

I am creating a Xamarin Cross Platform Application in it, I have replaced the Portable Class Library with .Net Standard 2.0. From this project we are making a call to web service hosted on azure service fabric cluster (this service is hosted using self signed certificate).I am getting the following error while making a connection request -

InnerException {System.Runtime.InteropServices.COMException (0x80072F0D): The text associated with this error code could not be found.

The certificate authority is invalid or incorrect

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpHandlerToFilter.d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.DiagnosticsHandler.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClientHandler.d__111.MoveNext()} System.Exception {System.Runtime.InteropServices.COMException}

I think the self-signed SSL causing the problem, in general in order to bypass certificate we use following code snippet

ServicePointManager.ServiceCertificateValidationCallback += (o, c, ch, er) => true;

And it works for Console Application targeting .Net framework 4.6.1 but this code doesn't seem to work for .Net Standard 2.0 and .Net Core 2.0 apps. Do we have to do something different in order to bypass certificates in .Net Standard and .Net Core apps. Following is the sample code area that is producing this error

IHubProxy _hub;
string url = @"https://www.xyz:com:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("MyHub");
connection.Start().Wait();
c#
.net
ssl
xamarin.forms
.net-standard-2.0
asked on Stack Overflow Nov 22, 2017 by Shubham Awasthi

2 Answers

1

ServicePointManager is not available in .NET Core, but you should be able to set a custom delegate using the HttpClientHandler.ServerCertificateCustomValidationCallback property using System.Net.Http 4.1+.

See Add support for custom validation of server SSL Certificate to HttpClient API and System.Net.Http 4.1 Contract Revisions on dotnet/corefx GitHub.

answered on Stack Overflow Dec 17, 2017 by desmondgc
1

You can simply write this in your platform specific projects like in Main Activity / App Delegate:

ServicePointManager.ServerCertificateValidationCallback +=
     (sender, cert, chain, sslPolicyErrors) => true;
answered on Stack Overflow Dec 19, 2017 by Adit Kothari • edited Apr 26, 2019 by Raziel

User contributions licensed under CC BY-SA 3.0