Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the IP address of the client connecting to a C# .NET WebAPI application

I tried:

private const string HttpContext = "MS_HttpContext";
private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";

public static string GetClientIpAddress(HttpRequestMessage request)
{
    if (request.Properties.ContainsKey(HttpContext))
    {
        dynamic ctx = request.Properties[HttpContext];
        if (ctx != null)
        {
            return ctx.Request.UserHostAddress;
        }
    }

    if (request.Properties.ContainsKey(RemoteEndpointMessage))
    {
        dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
        if (remoteEndpoint != null)
        {
            return remoteEndpoint.Address;
        }
    }

    return null;
}

according to:

Retrieving the client's IP address in ASP.Net Web API

this is the combined approach which should be valid for self host and webapi host. Unfortunately I get null instead of the IP address.

I'm trying locally so I'd expect 127.0.0.1 or localhost as the IP address

like image 431
Gianluca Ghettini Avatar asked Jun 29 '16 09:06

Gianluca Ghettini


People also ask

How do I find the IP address of a client connected to a server?

For server IP address, the ServerSocket class uses the IP address of the local network interface through which it received the incoming request. Then, to obtain the remote client IP address, it decodes the IP header of the received TCP packet and uses the source address.

How do I find my client IP address in CMD?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.

How do I find my host C IP address?

Here is a simple method to find hostname and IP address using C program. gethostname() : The gethostname function retrieves the standard host name for the local computer. gethostbyname() : The gethostbyname function retrieves host information corresponding to a host name from a host database.

How do you find a user's IP address?

The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.


1 Answers

Here is an expanded version of what you have that works for me.

static class HttpRequestMessageExtensions {

    private const string HttpContext = "MS_HttpContext";
    private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
    private const string OwinContext = "MS_OwinContext";

    public static string GetClientIpString(this HttpRequestMessage request) {
        //Web-hosting
        if (request.Properties.ContainsKey(HttpContext)) {
            dynamic ctx = request.Properties[HttpContext];
            if (ctx != null) {
                return ctx.Request.UserHostAddress;
            }
        }
        //Self-hosting
        if (request.Properties.ContainsKey(RemoteEndpointMessage)) {
            dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
            if (remoteEndpoint != null) {
                return remoteEndpoint.Address;
            }
        }
        //Owin-hosting
        if (request.Properties.ContainsKey(OwinContext)) {
            dynamic ctx = request.Properties[OwinContext];
            if (ctx != null) {
                return ctx.Request.RemoteIpAddress;
            }
        }
        if (System.Web.HttpContext.Current != null) {
            return System.Web.HttpContext.Current.Request.UserHostAddress;
        }
        // Always return all zeroes for any failure
        return "0.0.0.0";
    }

    public static IPAddress GetClientIpAddress(this HttpRequestMessage request) {
        var ipString = request.GetClientIpString();
        IPAddress ipAddress = new IPAddress(0);
        if (IPAddress.TryParse(ipString, out ipAddress)) {
            return ipAddress;
        }

        return ipAddress;
    }

}

Assuming you are in a controller the above extension method allows for calls like:

HttpRequestMessage request = this.Request;

var ip = request.GetClientIpString();
like image 109
Nkosi Avatar answered Sep 27 '22 22:09

Nkosi