Security - Obtain Client IP Address | Website Hosted in Cloud Environment Across Load Balancer | MVC

Asked By santhosh kumar on 28-Mar-14 04:20 AM
Hi,

Please find the scenario below:-

1. ASP.Net MVC website hosted in cloud.
2. Need to capture client/customer ip address and send it across to the payment gateway provider.
3. As we know, there will be load balancer which stands between the client machine and the website hosted server.
4. Whenever we try to capture the client ip address, we obtain the load balancer ip address instead of the client ip address.

Kindly provide suggestions on how to proceed.

Thanks,
Santhosh
Robbe Morris replied to santhosh kumar on 28-Mar-14 08:35 AM
How are you currently capturing the client ip address?
santhosh kumar replied to Robbe Morris on 28-Mar-14 08:48 AM
Hi Robbie,

We use the below code in the action method:-

string test =  "  UserHostAddress"+ ": " + HttpContext.Request.UserHostAddress;
               test = test + " REMOTE_ADDR"+ ": " + HttpContext.Request.ServerVariables["REMOTE_ADDR"];

When we used this code in the test environment and configured the website in the IIS Server without load balancer. Things seems to work as expected.However, in the cloud it doesn't seem to work.

Thanks,
Santhosh
Robbe Morris replied to santhosh kumar on 28-Mar-14 09:02 AM
With NAT's these days, getting the true ip address of the end user is getting more and more difficult these days.  Especially if you are in a business to business type scenario.  While it is extra load, you could try JSON, grab it in the browser, and run an async http request back to the server with the ip address on the query string.

Unfortunately, in a load balanced environment, the Request.ServerVariables collection isn't going to get you want you want.  If you are running a session state manager (like ScaleOut Software) across the farm, you could capture it on the first request and store it in session to avoid subsequent http requests to capture it.
santhosh kumar replied to Robbe Morris on 28-Mar-14 09:43 AM
Thanks for your suggestions. Will try it once and update you.