I'm using SignalR to host a TCP server through Kestrel in .NET 6.0 and want to restrict access to the TCP server so that only certain IPs are allowed to connect. I've been doing some research, and when using SignalR in a Http context, this is actually pretty easy; Context.Features.Get<IHttpConnectionFeature>();
can be used to do this.
However, when attempting so in a TCP environment (which it has to be for compatibility reasons), this method always returns null
. I have been searching for alternative solutions, and digging into the data that is available to me, but unfortunately without any success. Therefore I am starting to wonder if anyone else knows of a way to do this? Or should I just consider to stop using SignalR for TCP alltogether if I need control over who is able to connect and who isn't?
A firewall would have been an alternative option, but since this is for a bouncer, and it is determined by the back-end who is allowed to connect and who isn't (based on database data), this is not a possibility either.
I'm using Microsoft.AspNetCore.SignalR
. Thanks in advance for your help!
It turned out to be a different feature. Upon inspecting the Context.Features
while debugging, it turned out that there was a IConnectionSocketFeature
in the list of features.
When using Context.Features.Get<IConnectionSocketFeature>().Socket.RemoteEndPoint
you can boil it all down to the IP Address.