2017-10-16 203 views
0

嗨,我試圖讓我的本地IP4地址,但結果只返回IIS服務器無法獲取本地ip4的地址只有服務器地址

這裏的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

它應該返回我的本地IP地址而不是我的Web服務器 – samin

回答

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