Last active
May 19, 2016 03:46
-
-
Save praveensewak/0393cb7bfcea2aedd3eee1c363d5ee92 to your computer and use it in GitHub Desktop.
Get the IP address of the remote host in Web API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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