Skip to content

Instantly share code, notes, and snippets.

@praveensewak
Last active May 19, 2016 03:46
Show Gist options
  • Save praveensewak/0393cb7bfcea2aedd3eee1c363d5ee92 to your computer and use it in GitHub Desktop.
Save praveensewak/0393cb7bfcea2aedd3eee1c363d5ee92 to your computer and use it in GitHub Desktop.
Get the IP address of the remote host in Web API
private string GetClientIp(HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment