0

I am running an application that has to get the current user IP address, the functionality seems to be working when running the app from VS, however it breaks completely when deployed on IIS, here is the code to get the user IP

 public static string GetUserIP()
 {
   string strHostName = "";
   strHostName = System.Net.Dns.GetHostName();

   IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

   IPAddress[] addr = ipEntry.AddressList;

   return ipEntry.AddressList.Last().ToString();
}

I have another method which uses the return value from the GetUserIP() method, here is the code

private void SaveSmsDetails(string cellNumber, string message, string userLogonName) { string[] userIP = IPAddressGetter.GetUserIP().Split('.'); }

Now this method is throwing a an index out of range exception, this means the GetUserIP method does not returning any value hence the Split method is falling apart, is there anything I need to do or change or perhaps something must be done on the IIS server. Note: IIS version is 8.5.9

Mronzer
  • 399
  • 1
  • 7
  • 18
  • 1
    At best, that's getting the _server_ IP? Am I misunderstanding something here? I'm going to go out on a limb and guess that the host name doesn't match the DNS entry for the server. – ProgrammingLlama Jun 01 '17 at 08:04
  • 1
    If you actually do want the _user_ IP, you should refer to [this](https://stackoverflow.com/questions/19285957/how-to-get-the-public-ip-address-of-a-user-in-c-sharp) – ProgrammingLlama Jun 01 '17 at 08:10

1 Answers1

0

MSDN says: If the host name could not be found, the SocketException exception is returned with a value of 11001 (Windows Sockets error WSAHOST_NOT_FOUND). This exception can be returned if the DNS server does not respond. This exception can also be returned if the name is not an official host name or alias, or it cannot be found in the database(s) being queried.

Details here: Dns.GetHostEntry Method (String)

So, make sure that the known issues/scenarios are properly handled.

Further, try GetHostByName instead of GetHostEntry

source here