Click here to Skip to main content
15,570,202 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on application in WinForms, and today certificate on remote server has been expired, so I was unable to connect to my WebSocketServer.

Question : I am wondering, is there any method or property that can be used, to connect on WebSocketServer even if certificate is invalid, and how to ignore certificate errors ?

What I have tried:

Here is part of code related to opening connection to server:

C#
public async Task OpenConnectionAsync(CancellationToken token)
        {
            try
            {
                wsClient.Options.KeepAliveInterval = TimeSpan.Zero; //Set keep alive interval

                wsClient.Options.DangerousDeflateOptions = new WebSocketDeflateOptions //Add options if compression is needed
                {
                    ServerContextTakeover = true,
                    ClientMaxWindowBits = 15
                    
                };
               
                await wsClient.ConnectAsync(new Uri("wss://link-to-server/"), token).ConfigureAwait(false);
            }
            catch(WebSocketException wse)
            {
                MessageBox.Show("We have found that you have error with WebSocketServer: " + wse.Message + Environment.NewLine);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message + Environment.NewLine);
            }
        }
Posted
Updated 3-Sep-22 3:58am

1 solution

Hi,

You can use the call back to indicate true and therefore accept all certificates in your application.

Here's the VB code to change to C#

ServicePointManager.ServerCertificateValidationCallback = Function(s, c, h, e) True


If you walk thru in debug you'll see the function active when the connection starts.
 
Share this answer
 
Comments
Jakobson 2022 3-Sep-22 10:11am    
Hello, thank you for pointing out, I've read now documentation related to your solution here specially for C# https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.servercertificatevalidationcallback?view=net-6.0

I will accept this as solution, you pointed me in right direction

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900