How to get remote client IP address under SignalR

Take ASP.NET Core 3.1 as an example

LAI TOCA
.Net Programming
3 min readJun 17, 2021

--

Photo from: https://wateroxconsulting.com/archives/whitelist/

In some scenarios, we would like to create a white list to allow the specific end-user to access the diverse resource(s) under SignalR. So if we could retrieving request information something like IP address, user identity…that we might apply some access controlling logic during data transmission among SignralR server and client(s).

For capturing the client remote address, we could just simply add the below code under EventHub.cs:

Please note that the RemoteIpAddress might not reflect the actual(real) client address if your SignalR server was under proxy (for instance nginx server)

IHttpConnectionFeature

So how to do that we could get the real end user’s IP address?

We have two approaches to deal with the case:

One solution was to register the global middleware and acting with correspond control as your wish. Sample code is below:

The other solution was more instruction, override the method: OnConnectedAsync on the IEventHub, sample code as below:

Finally, please remember to configure your proxy server to passing/forward through the client-side IP address or you will unable to capture the real client’s information behind the proxy.

Reference

--

--

LAI TOCA
.Net Programming

Coding for fun. (Either you are running for food or running for being food.)