简体   繁体   English

如何在 C#/asp.net/MVC 5 中获取用户的 IP 地址?

[英]How can I get a user's IP address in C#/asp.net/MVC 5?

The following code works OK locally, but it will only get the server's IP (if I'm correct).以下代码在本地工作正常,但它只会获取服务器的 IP(如果我是正确的)。

try
{
    string externalIP;
    externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
    externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                 .Matches(externalIP)[0].ToString();
    model.IpCreacion = externalIP;
}
catch { }

I can't test this right now, because the two guys at my office that can make this as a public URL for testing on a server aren't here today.我现在无法对此进行测试,因为我办公室里可以将其作为公共 URL 以在服务器上进行测试的两个人今天不在。 The code is in the controller of the project, so it runs on the server every time a client executes the app, it's not actually the client who is getting the IP address.该代码在项目的控制器中,因此每次客户端执行应用程序时它都会在服务器上运行,实际上获取IP地址的并不是客户端。

How can I make the client get his IP address, instead of the server, executing the code I just showed?如何让客户端获取他的 IP 地址,而不是服务器,执行我刚刚展示的代码?

If I managed to put this functionality in a view, would it work as I'm intending to?如果我设法将此功能放在视图中,它会按我的意图工作吗?

UPDATE: I tried other methods posted as answers, like更新:我尝试了其他作为答案发布的方法,例如

string ip = System.Web.HttpContext.Current.Request.UserHostAddress;

and

model.IpCreacion = null;
model.IpCreacion = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

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

but now I'm only getting ::1 as a result.但现在我只得到::1结果。 Which didn't happen before, as I was getting a correct IP address.以前没有发生过,因为我得到了正确的 IP 地址。

That gets only IP of server, because you send the request from Web Server to checkip.dyndns.org.这仅获取服务器的 IP,因为您将请求从 Web 服务器发送到 checkip.dyndns.org。

To get the client IP, you need to use JavaScript and do the same thing.要获取客户端 IP,您需要使用 JavaScript 并执行相同的操作。

$.get('http://checkip.dyndns.org/', function(data) {
    console.log(data); // client IP here.
})

UPDATED:更新:

If you need client IP Address in ASP.NET Core, you can inject this service如果你在 ASP.NET Core 中需要客户端 IP 地址,你可以注入这个服务

private IHttpContextAccessor _accessor;

And use it as并将其用作

_accessor.HttpContext.Connection.RemoteIpAddress.ToString()

Or in ASP.NET Framework或者在 ASP.NET 框架中

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

如果您想获取客户端 IP 地址,请访问 stackoverflow 中的以下帖子如何在 ASP.NET MVC 中获取客户端的 IP 地址?

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

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

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