Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .NET WebSocket Client with NTLM proxies?

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?

like image 965
RichardTheKiwi Avatar asked Aug 23 '16 03:08

RichardTheKiwi


People also ask

Do proxies support WebSockets?

Transparent proxies will usually by default forward SSL traffic. This will allow us to establish a WebSocket connection.

Does .NET support WebSockets?

. 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.


1 Answers

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.

like image 76
Brandon Griffin Avatar answered Oct 04 '22 17:10

Brandon Griffin