获取连接到C#.NET WebAPI应用程序的客户端的IP地址 [英] Get the IP address of the client connecting to a C# .NET WebAPI application

查看:3384
本文介绍了获取连接到C#.NET WebAPI应用程序的客户端的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过:

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;
}

根据:

检索客户端在ASP.Net Web API中的IP地址

这是对自托管主机和webapi主机应该有效的组合方法。不幸的是,我得到 null 而不是IP地址。

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

我正在尝试本地,所以我希望 127.0.0.1 localhost 作为IP地址

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

推荐答案

以下是您所拥有的扩展版本。

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();

这篇关于获取连接到C#.NET WebAPI应用程序的客户端的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆