0
votes

I have an ASP.Net Web API application hosted in Azure’s App Service scaled out using their auto scale/ARR. To read the client’s IP do I need to be looking at X-FORWARDED-FOR or can it be read as though there was no load balancer, like here: https://stackoverflow.com/a/22532924

Can’t seem to find docs on how it works.

1
Maybe you could try this one.docs.microsoft.com/en-us/dotnet/api/…George Chen

1 Answers

1
votes

I can just give you my test result.

I create a web app and manually set it to 2 instances.

My code:

public string GetIP()
{
    string HTTP_X_FORWARDED_FOR = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    string REMOTE_ADDR = Request.ServerVariables["REMOTE_ADDR"];
    string UserHostAddress = Request.UserHostAddress;
    string WEBSITE_INSTANCE_ID = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
    return Newtonsoft.Json.JsonConvert.SerializeObject(new { HTTP_X_FORWARDED_FOR = HTTP_X_FORWARDED_FOR, REMOTE_ADDR = REMOTE_ADDR, UserHostAddress = UserHostAddress, WEBSITE_INSTANCE_ID = WEBSITE_INSTANCE_ID });
}

Result:

// First request from 13.70.19.163
{"HTTP_X_FORWARDED_FOR":"13.70.19.163:50331","REMOTE_ADDR":"13.70.19.163","UserHostAddress":"13.70.19.163","WEBSITE_INSTANCE_ID":"2b4ef85523c8628779b336b1ae7771fb0c5e289014ef47912d911dceb47ba032"}

// Second request from 65.52.178.194
{"HTTP_X_FORWARDED_FOR":"65.52.178.194:31190","REMOTE_ADDR":"65.52.178.194","UserHostAddress":"65.52.178.194","WEBSITE_INSTANCE_ID":"2b4ef85523c8628779b336b1ae7771fb0c5e289014ef47912d911dceb47ba032"}

// Another request from 223.67.26.144. This request is redirected to a different instance
{"HTTP_X_FORWARDED_FOR":"223.67.26.144:2732","REMOTE_ADDR":"223.67.26.144","UserHostAddress":"223.67.26.144","WEBSITE_INSTANCE_ID":"76deb6ae830639a9e41a93b4cc30ee9961483023510e33e97582dc1e2ac11a99"}

Conclusion:

Request.ServerVariables["HTTP_X_FORWARDED_FOR"], REMOTE_ADDR and UserHostAddress all can get correct client IP.