【发布时间】:2017-04-29 11:46:12
【问题描述】:
我在 asp.net c# 中工作,因为我需要获取客户端 IP 地址来显示客户端 IP。 我在 IIS 7 中托管我的项目,使用静态 ip 我可以连接我的应用程序..
我必须使用以下代码获取客户端 IP。但我无法获得正确的 IP 地址..
每次我得到这个ip 192.168.1.18..
我使用以下代码
private void GetIP()
{
string userip = Request.UserHostAddress;
if (Request.UserHostAddress != null)
{
Int64 macinfo = new Int64();
string macsrc = macinfo.ToString("X");
if (macsrc == "0")
{
if (userip == "127.0.0.1")
{
//ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('Visited Localhost')", true);
lblIPAddress.Text = userip;
}
else
{
lblIPAddress.Text = userip;
}
}
}
}
我也在使用以下代码,但它显示了托管的 IP 地址,例如 192.168.1.5,我在服务器中托管我的项目..
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
//if (ip.AddressFamily == AddressFamily.InterNetwork)
if (ip.AddressFamily != AddressFamily.InterNetworkV6)
{
return ip.ToString();
}
}
throw new Exception("Local IP Address Not Found!");
}
我需要正确的客户端IP地址,任何人帮助
【问题讨论】:
标签: c# asp.net ip client ip-address