I have a web API where I want to capture all incoming request public IP address inside my controller function.
1 Answer
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
3 Comments
manas sahu
I already tried with this. It's returning the hosted IP address not the public IP address
Prasad Telkikar
@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.
manas sahu
please check this API. I want IP address the same which is being returned by this API api.ipify.org?format=json @Prasad telkikar
HTTPRequestMessageor fromRequest?