4

I want to specify an ssl certificate for a web socket client to use when connecting to a web socket server.

The only constructor for a System.Net.WebSockets.ClientWebSocket has 0 parameters. I would expect there to be an override with (Options) as a parameter but there is not.

The ConnectAsync method has 2 parameters: Uri and CancellationToken. I would expect there to be an override with (Uri, CancellationToken, Options) but there is not.

The ClientWebSocket Options property is get but not set, so i cannot call

ws.options = MyClientWebSocketOptions

There is no SetOptions method on the ClientWebSocket class.

The ClientWebSocketOptions class has a ClientCertificates property that is is set-able, so I have assumed this is the way to specify the certificate.

I can create a ClientWebSocketOptions with the correct certificate, but i need to know how to make a ClientWebSocket use those options.

1
  • 2
    +1 because its rare to see paragraphs, a well formatted post, links to the relevant information and well thought out / structured question
    – TheGeneral
    Aug 27, 2020 at 23:54

1 Answer 1

4

Sure, you can't do this: ws.options = MyClientWebSocketOptions, but you can do this:

var webSocket = new ClientWebSocket();
webSocket.Options.ClientCertificates.Add(new 
    System.Security.Cryptography.X509Certificates.X509Certificate());

It may be a getter, but that's fine as you can just modify the ClientWebSocketOptions the getter returns that the implementation instantiates for you.

Once you call ConnectAsync, that Options property becomes "read only" and you can no longer modify it (from what I remember)

1
  • 1
    Ah ha! I see! I will give this a try! Thanks. Will accept your answer if it works. Aug 28, 2020 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.