Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Edited Unassigned: Provide request message extension for getting client IP address [1116]

$
0
0
__Scenario__:
Users would like to log the IP address of the remote client making the request.

__Issue__:
Its not easily discoverable as to how to retrieve the value. Users would first try to check the current HttpRequestMessage instance for any presence of this, but it does not have this detail. Also if you look at the workaround, the user has to handle different ways for all the hosts to retrieve the data.

__Impact__:
Low. User experience is effected. But this question has come up many times now in Stackoverflow.
* http://stackoverflow.com/questions/17364801/how-to-get-ipaddress-and-useragent-in-asp-net-web-api-get-methods/17365027?noredirect=1#comment25212229_17365027
* http://stackoverflow.com/questions/15712720/get-web-api-consumer-ip-address-and-hostname-in-asp-net-c-sharp
* http://stackoverflow.com/questions/9565889/get-the-ip-address-of-the-remote-host

__Workaround__:
```
private string GetClientIp(HttpRequestMessage request)
{
// OWIN Self host
IDictionary<string, object> owinEnvProperties = request.Properties["MS_OwinEnvironment"] as IDictionary<string, object>;
if(owinEnvProperties != null)
{
return owinEnvProperties["server.RemoteIpAddress"].ToString();
}
// Web Host
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
// WCF Self Host
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
}
else
{
return null;
}
}
```

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>