4

I have a web API where I want to capture all incoming request public IP address inside my controller function.

7
  • Do you want to get client's IP address from HTTPRequestMessage or from Request? Commented Nov 16, 2017 at 10:59
  • Could you define what you mean by public IP address? You have already been told how in the answer below. Commented Nov 16, 2017 at 12:25
  • I want client internal IP not the hosted IP address , For reference please search whats my IP in google you will get your internal IP address.@Crowcoder Commented Nov 16, 2017 at 13:26
  • No, that is the public IP. You will never get the internal IP unless the client and server are on the same network. Commented Nov 16, 2017 at 13:27
  • I want that public Ip which is showing in what my IP in Asp.net web form the below code is working fine but I want in web API controller @Crowcoder Commented Nov 16, 2017 at 13:50

1 Answer 1

2

Check below code this must return you IP address of client

 protected string GetUser_IP() { string VisitorsIPAddr = string.Empty; if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) { VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); } else if (HttpContext.Current.Request.UserHostAddress.Length != 0) { VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress; } return VisitorsIPAddr; } 

For more help: Get public IP Address

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

3 Comments

I already tried with this. It's returning the hosted IP address not the public IP address
@manassahu, I answered 2 similar questions still requester is not satisfied my answer. Whenever you get working function for reading client IP , just post it. your answer will help me as well as to others.
please check this API. I want IP address the same which is being returned by this API api.ipify.org?format=json @Prasad telkikar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.