繁体   English   中英

C#IP地址始终为:: 1

[英]C# IP Address always ::1

我尝试了以下说明的方法,无论如何,在本地和服务器端,IP地址始终为:: 1,

这是我尝试过的代码;

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

有谁知道为什么以及如何解决它?

提前致谢!

下面的代码对我有用。

当我在localhost中运行应用程序时,我总是得到:: 1。 但是,它在服务器端工作得很好。 该应用程序正在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.

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