My goal is to use a WebSocket .Net client implementation (i.e. not a browser) to connect to a WebSocket over a corporate proxy that requires NTLM authentication. So far, all the solutions (e.g. websocket-sharp-with-proxy, websocket4net) come up short.
I have tried these:
How to use proxies with the WebSocket4Net library
https://stackoverflow.com/a/2587330/573261
As far as I know, the author has not updated this discussion on adding NTLM proxy support. http://websocket4net.codeplex.com/discussions/448172
This question tried to ask why client implementations do not support proxies, but it was not answered, and did not shed any new light on why nobody wants to approach the problem.
So back to my question, how does one use WebSocket technology in a client app behind an NTLM proxy?
Transparent proxies will usually by default forward SSL traffic. This will allow us to establish a WebSocket connection.
. NET 7 introduced Websockets over HTTP/2 support for Kestrel, the SignalR JavaScript client, and SignalR with Blazor WebAssembly. HTTP/2 WebSockets use CONNECT requests rather than GET, so your own routes and controllers may need updating.
The ClientWebSocket
object found in System.Net.WebSockets
contains an Options
property which allows you to configure proxy settings.
var ws = new System.Net.WebSockets.ClientWebSocket();
await ws.ConnectAsync(new Uri("destination.com"), CancellationToken.None);
ws.Options.Proxy = new WebProxy("example.com")
{
Credentials = new NetworkCredential("userName", "password")
};
The NetworkCredential
object is used for password based authentication and may provide what you need. You can also try setting Credentials
on the Options
property of the WebSocket
object like this if the above doesn't work.
ws.Options.Credentials = new NetworkCredential("userName", "password");
I do not have access to this type of environment, so I have not actually tried what you are trying to do and am not sure if this will help. But, this looks like it will work to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With