doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
729 stars 125 forks source link

Create connection to SSL with rejectUnauthorized: true #236

Closed bhemala closed 2 years ago

bhemala commented 2 years ago

Hi, how can I connect to socket.IO with SSL and selfsigned certificate? In JS code I use this:

const socket = io.connect(urlToBridge, {
    extraHeaders: {
        token: tokenFromUser,   
    },
    rejectUnauthorized : false,
    secure: true,

});

In csharp I do not know how to add rejectUnauthorized and secure true.

 var client = new SocketIO("https://localhost:443", new SocketIOOptions
            {
                ExtraHeaders = new Dictionary<string, string>
                {
                    {"type", "server" },
                    {"token", tokenFromUser
                },               
            });

Thanks

doghappy commented 2 years ago

Is it possible to ignore these two options?

bhemala commented 2 years ago

If I ignore them and try to connect to SSL with selfsigned certificate I get this excepations:

System.Security.Authentication.AuthenticationException HResult=0x80131501 Message=The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors Source=System.Private.CoreLib StackTrace: at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

I tried:

HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
client.HttpClient = new HttpClient(httpClientHandler);

But it does not helped.

doghappy commented 2 years ago

pls try:

var client = new SocketIO("http://localhost:11000/");
client.ClientWebSocketProvider = () =>
{
    var clientWebSocket = new DefaultClientWebSocket
    {
        ConfigOptions = o =>
        {
            var options = o as ClientWebSocketOptions;

            options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
            {
                Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors);
                return true;
            };
        }
    };
    return clientWebSocket;
};
bhemala commented 2 years ago

It works! thanks

bhemala commented 2 years ago

Hi, I have portated my app from .NET Core to .NET Framework 4.7.2 and I am not able to set: options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors); return true; };

Method RemoteCertificateValidationCallback does not exists in:

`#region Assembly System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.dll

endregion

using System.Security.Cryptography.X509Certificates;

namespace System.Net.WebSockets { // // Summary: // Options to use with a System.Net.WebSockets.ClientWebSocket object. public sealed class ClientWebSocketOptions { ............. }`

How can I use it in .NET Framework?

Thanks

doghappy commented 2 years ago

For .NET Framework please use the following code instead

    System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
    {
        Console.WriteLine(sslPolicyErrors);
        // Note it will ignore all SSL errors
        return true;
    };
bhemala commented 2 years ago

Excellent, it works. Thanks!

sitihawasabri commented 2 years ago

Hi @bhemala , may I have similar issue with yours. I tried to run your code but I have this error instead @doghappy .

image

'SocketIO' does not contain a definition for 'ClientWebSocketProvider' and no accessible extension method 'ClientWebSocketProvide' accepting a first argument of type 'SocketIO' could be found (are you missing a using directive or an assembly reference?)

May I know which SocketIOClient.NetFx version you use right now? I currently use the latest version: 2.0.2.10

bhemala commented 2 years ago

Hi @sitihawasabri, for .NET Framework I do not use client.ClientWebSocketProvider. I skipped this part and use System.Net.ServicePointManager.ServerCertificateValidationCallback instead.

var client = new SocketIO("http://localhost:11000/");
 System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
    {
        Console.WriteLine(sslPolicyErrors);
        // Note it will ignore all SSL errors
        return true;
    };
sitihawasabri commented 2 years ago

Okay noted, thank you so much!