Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HttpRequestMessage.GetClientIpAddress for ASP.NET MVC 4 / Web API
// 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;
}
}
}
}
@Rykie

This comment has been minimized.

Copy link

@Rykie Rykie commented Oct 28, 2012

Very nice, but it could be a little cleaner:

public static string GetClientIpAddress(this HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
        return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;

    if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
        return ((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address;

    return "IP Address Unavailable";    //here the user can return whatever they like
}
@DannyGB

This comment has been minimized.

Copy link

@DannyGB DannyGB commented Feb 7, 2013

I'd add one more change:

Instead of Casting to HttpContextWrapper which is not easy to Mock cast to HttpContextBase:

public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
return ((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress;

if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
    return ((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address;

return "IP Address Unavailable";    //here the user can return whatever they like

}

@seththetech

This comment has been minimized.

Copy link

@seththetech seththetech commented Aug 8, 2013

Another change which may be considered useful is to return type IPAddress instead of string.

    public static IPAddress GetClientIpAddress(this HttpRequestMessage request)
    {
        if (request.Properties.ContainsKey("MS_HttpContext"))
        {
            return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress);
        }
        if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
        {
            return IPAddress.Parse(((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address);
        }
        throw new Exception("Client IP Address Not Found in HttpRequest");
    }
@eallegretta

This comment has been minimized.

Copy link

@eallegretta eallegretta commented Nov 19, 2014

Another change with support for OWIN

public static IPAddress GetClientIpAddress(this HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
        return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress);
    }

    if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
    {
        return IPAddress.Parse(((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address);
    }

    if (request.Properties.ContainsKey("MS_OwinContext"))
    {
        return IPAddress.Parse(((OwinContext)request.Properties["MS_OwinContext"]).Request.RemoteIpAddress);
    }

    throw new Exception("Client IP Address Not Found in HttpRequest");
}
@Ofer-Gal

This comment has been minimized.

Copy link

@Ofer-Gal Ofer-Gal commented Oct 9, 2015

UserHostName '((((HttpContextBase)request.Properties["MS_HttpContext"])).Request).UserHostName' threw an exception of type 'System.ArgumentException' string {System.ArgumentException}

@M-Yankov

This comment has been minimized.

Copy link

@M-Yankov M-Yankov commented Apr 27, 2018

UserHostName '((((HttpContextBase)request.Properties["MS_HttpContext"])).Request).UserHostName' threw an exception of type 'System.ArgumentException' string {System.ArgumentException}

I'm having same problem with Argument exception. It happens when I wrap the code in Task.Run(... )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment