[英]C# IP Address always ::1
I've tried something explained below, somehow, IP Address is always ::1, on both local and server side, 我尝试了以下说明的方法,无论如何,在本地和服务器端,IP地址始终为:: 1,
Here are the codes, I tried; 这是我尝试过的代码;
m_CallerIP = string.IsNullOrEmpty(req.ServerVariables["HTTP_CLIENT_IP"]) ? req.UserHostAddress : req.ServerVariables["HTTP_CLIENT_IP"];
string Port = string.IsNullOrEmpty(req.ServerVariables["HTTP_CLIENTPORT"]) ? req["HTTP_CLIENTPORT"] : req.ServerVariables["CLIENTPORT"];
string asd2 = String.IsNullOrEmpty(req.ServerVariables["REMOTE_ADDR"]) ? req.UserHostAddress : req.ServerVariables["REMOTE_ADDR"];
string asd23 = String.IsNullOrEmpty(req.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? req.UserHostAddress : req.ServerVariables["HTTP_X_FORWARDED_FOR"];
string asd4 = req.UserHostAddress;
IPAddress address = IPAddress.Parse(m_CallerIP);
IPAddress address1 = IPAddress.Parse(asd2);
IPAddress address2 = IPAddress.Parse(asd23);
IPAddress address3 = IPAddress.Parse(asd4);
Is there anyone to know why and how to solve it ? 有谁知道为什么以及如何解决它?
Thanks in Advance! 提前致谢!
The code bellow works for me. 下面的代码对我有用。
When I run the app in localhost I always get ::1. 当我在localhost中运行应用程序时,我总是得到:: 1。 But, it works greate on server side. 但是,它在服务器端工作得很好。 The app is running in Azure. 该应用程序正在Azure中运行。
string ip = "";
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
ip = ip.Split(',').Last().Trim();
else
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (ip == "::1")
ip = "";
if (ip.IndexOf(':') > 0)
ip = ip.Substring(0, ip.IndexOf(':'));
return ip;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.