Enabling Reverse DNS Lookup on request.userhostname

Discussion in 'ASP.NET / ASP.NET Core' started by 1PabloAngel, Dec 6, 2010.

  1. Hi,

    I'm trying to retrieve client host name information to record into a database, but request.userhostname only returns an IP address... I'm aware that you should be able to enable IIS to automatically complete a reverse DNS Lookup which gives you the host name... but can't figure out how to do it. Could anyone give me a few pointers please?
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    Do you mean the HTTP log file?
     
  3. This is what I use but it has nothing to do with IIS settings. Be aware it doesn't always work because a reverse DNS lookup based on IP address is sometimes not possible.

    Code:
    string clientHostName = "Undetermined";
    
    IPHostEntry iphostEntry;
    try
    {
    iphostEntry = Dns.GetHostEntry(Request.UserHostAddress);
    clientHostName = iphostEntry != null ? iphostEntry.HostName : clientHostName;
    }
    catch (SocketException)
    {
    iphostEntry = Dns.Resolve(Request.UserHostAddress);
    clientHostName = iphostEntry != null ? iphostEntry.HostName : clientHostName;
    }
    
     

Share This Page