0

The method below is used to get the client machine name in MVC 4. I've noticed that for some clients it returns an empty string, any idea why this could happen?

Is there another,more reliable way to get the clients machine name?

private string GetWorkstation()
    {
        string ip = Request.UserHostName;
        IPAddress myIP = IPAddress.Parse(ip);
        IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
        List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
        return compName.First().ToUpper();
    }
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • 2
    Did you read the question?@Jonesy – Denys Wessels Aug 19 '14 at 12:36
  • Check this http://stackoverflow.com/questions/12585717/asp-net-request-userhostname-not-containing-hostname – Deepu Madhusoodanan Aug 19 '14 at 12:48
  • Do you know when it fails to get the hostname, can you perhaps log `ip` to get a hint? – Patrick Aug 19 '14 at 12:48
  • @Deepu: The OP is already using `Dns.GetHostEntry`, how does that question help? – Patrick Aug 19 '14 at 12:49
  • @Patrick IP addresses may change if they are given by DHCP so are not a valid way to determine the client. – DavidG Aug 19 '14 at 12:50
  • @Denis Are these internal addresses (i.e. an Intranet application) or external Internet addresses? – DavidG Aug 19 '14 at 12:51
  • @DavidG: That's why I was asking what `Request.UserHostName` is when the Dns lookup fails to give the expected value. The question at the moment is difficult to answer, since we have no way of knowing what the intermediary values are. – Patrick Aug 19 '14 at 12:53

1 Answers1

1

Why don't you try with System.Net.Dns.GetHostName()

peev
  • 302
  • 3
  • 13