Configure ClientWebSocket's options before calling ConnectAsync. Starting the connection first and assigning the proxy afterward is too late for that handshake.

using var socket = new ClientWebSocket();
socket.Options.Proxy = new WebProxy("http://203.0.113.10:8080")
{
    Credentials = new NetworkCredential(proxyUser, proxyPassword)
};
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(20));
await socket.ConnectAsync(new Uri("wss://example.com/socket"),
                          deadline.Token);

The addresses and account variables are illustrative. The required namespaces include System.Net, System.Net.WebSockets, and System.Threading. Use the credential type and authentication mechanism supported by the runtime, platform, and proxy service in your deployment.

The WebProxy credentials belong to the proxy. ClientWebSocketOptions also has a separate Credentials property for the WebSocket server's authentication. Assigning one does not automatically satisfy the other party.

For a secure WebSocket connection, a rejected proxy tunnel can prevent the destination's WebSocket handshake from starting at all. Separate proxy connection and authentication evidence from the destination's HTTP upgrade response and later WebSocket traffic.

A successful connection is only the start of the session. Keep receive/send cancellation and resource cleanup explicit for the application's lifetime; the setup deadline in this sketch is not a complete policy for every subsequent message.

Artihkkalat mat laktásit