0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.