您好我正在尝试获取我的本地ip4地址,但结果只返回IIS服务器的IP
这是我的代码:
public string GetLocalIPv4(NetworkInterfaceType _type)
{
string output = "";
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
{
if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
{
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
output = ip.Address.ToString();
}
}
}
}
return output;
}
string IPAddress = GetLocalIPv4(NetworkInterfaceType.Ethernet);
IIS身份验证是我只启用Windows身份验证,因为我正在使用窗口身份验证。但为什么返回的ip不是我的本地ip4而是IIS服务器地址?
答案 0 :(得分:0)
一般来说:
HttpContext.Current.Request.UserHostAddress;
乏味的方法:
using System.Net.Sockets;
foreach(IPAddress ip in Dns.GetHostEntry(System.Environment.MachineName.AddressList){
if(ip.AddressFamily == AddressFamily.InterNetwork)
Console.WriteLine("IPV4"+ip.ToString());
if(ip.AddressFamily == AddressFamily.InterNetworkV6)
Console.WriteLine("IPV6"+ip.ToString());
来源:https://msdn.microsoft.com/en-us/library/system.net.sockets.addressfamily(v=vs.110).aspx