简体   繁体   English

如何在MVC中获取客户端ip地址

[英]How to get client ip address in MVC

I would like to pull out the client's PC's IP address.我想拉出客户端PC的IP地址。 I have tried below approaches but it always returns "::1"我尝试了以下方法,但它总是返回“::1”

1. var test = this.Request.ServerVariables.Get("REMOTE_ADDR");

2. request.UserHostAddress;

Any inputs?任何输入?

You can get the IP address like this in your Controller method:您可以在Controller方法中获取这样的 IP 地址:

If the client machine is behind a proxy server, we can check for the variable HTTP_X_FORWARDED_FOR :如果客户端机器在代理服务器后面,我们可以检查变量HTTP_X_FORWARDED_FOR

string ip;
ip =  System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
    ip =  System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}

If the IP Address is not found in the HTTP_X_FORWARDED_FOR server variable, it means that it is not using any Proxy Server and hence the IP Address is now checked in the REMOTE_ADDR server variable.如果在HTTP_X_FORWARDED_FOR服务器变量中找不到 IP 地址,则意味着它没有使用任何代理服务器,因此现在在REMOTE_ADDR服务器变量中检查 IP 地址。

Note: On your local machine, the IP address will be shown as ::1 which is an IPV6 loopback address (ie localhost).注意:在您的本地计算机上,IP 地址将显示为::1 ,这是一个 IPV6 环回地址(即 localhost)。 An IPV4 address would be 127.0.0.1 . IPV4 地址为127.0.0.1

This is because in such case the Client and Server both are the same machine.这是因为在这种情况下,客户端和服务器都是同一台机器。 When the application is deployed on the server, then the result will be different.当应用程序部署在服务器上时,结果会有所不同。

The below code should give you collection of client IP Addresses.下面的代码应该为您提供客户 IP 地址的集合。 If not, please post your code snippet so we can verify:如果没有,请发布您的代码片段,以便我们验证:

public static System.Collections.Generic.IReadOnlyCollection<System.Net.IPAddress> GetIpAddresses()
{
    var ipAddresses = new List<System.Net.IPAddress>();

    foreach (var values in GetValues(System.Web.HttpContext.Current.Request.Headers, "X-Forwarded-For"))
    {
        foreach (var ipv in values.Split(new char[] { ',', ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
        {
            if (ipv.Contains(":"))
            {
                var array = ipv.Split(':');
                ipAddresses.Add(array.Length == 2 ? System.Net.IPAddress.Parse(array[0].Trim()) : System.Net.IPAddress.Parse(ipv.Trim()));
            }
            else
            {
                ipAddresses.Add(System.Net.IPAddress.Parse(ipv));
            }
        }
    }

    if (request.UserHostAddress != null)
    {
        if (request.UserHostAddress.Contains(":"))
        {
            var array = request.UserHostAddress.Split(':');
            ipAddresses.Add(array.Length == 2 ? System.Net.IPAddress.Parse(array[0].Trim()) : System.Net.IPAddress.Parse(request.UserHostAddress.Trim()));
        }
        else
        {
            ipAddresses.Add(System.Net.IPAddress.Parse(request.UserHostAddress));
        }
    }
    return ipAddresses;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM