Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menacestudio/5732672 to your computer and use it in GitHub Desktop.
Save menacestudio/5732672 to your computer and use it in GitHub Desktop.
// Original idea: http://stackoverflow.com/questions/9565889/get-the-ip-address-of-the-remote-host
using System.Net.Http;
using System.ServiceModel.Channels;
using System.Web;
namespace CrowSoftware.Api
{
public static class HttpRequestMessageHelper
{
public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
}
else
{
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment