Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore SSL certificate in .Net Standard 2.0

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();
like image 568
Shubham Awasthi Avatar asked Oct 28 '22 22:10

Shubham Awasthi


2 Answers

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.

like image 71
desmondgc Avatar answered Nov 01 '22 22:11

desmondgc


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

ServicePointManager.ServerCertificateValidationCallback +=
     (sender, cert, chain, sslPolicyErrors) => true;
like image 45
Adit Kothari Avatar answered Nov 01 '22 20:11

Adit Kothari