Victor's Blog

Live as if you were to die tomorrow. Learn as if you were to live forever


How to retrieve client IP address in Web API?

If you are doing web hosting, you can retrieve the ip address like:

var clientAddress = HttpContext.Current.Request.UserHostAddress;

Or if you do self-hosting, you can use this

object property;
Request.Properties.TryGetValue(typeof(RemoteEndpointMessageProperty).FullName, out property);
RemoteEndpointMessageProperty remoteProperty = property as RemoteEndpointMessageProperty;