2

How to get Client's computer name from WCF asmx C#?

I have tried this way but it doesn't work at all.

  1. This is a best solution that I've thought of but it doesn't work ... okay the result is to return the Client's computer name but sometimes it returns Computer name A and sometimes it's Computer name B.

    string[] computer_name = System.Net.Dns.GetHostEntry(
       HttpContext.Current.Request.ServerVariables["remote_addr"])
         .HostName.Split(new Char[] { '.' });
    logData.ComputerName = computer_name[0].ToString();
    
  2. The result is Computer name from WCF not Client.

    System.Environment.MachineName
    
  3. The same result as 1 but returns Empty sometime.

    System.Net.Dns.GetHostByName("LocalHost").HostName.
    
  4. Returns Null.

    OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name
    
  5. Return IP,IP and Client's user

     loginRequest.ServerVariables["remote_addr"],
     Request.ServerVariables["remote_host"],
     Request.ServerVariables["remote_user"]
    
Mark Mikofski
  • 19,398
  • 2
  • 57
  • 90
  • Is the client machine on the same network as the client machine? – Kenneth K. Aug 02 '16 at 02:24
  • Check this link if it helps - http://stackoverflow.com/questions/5034660/how-to-retrieve-the-clients-machine-name-from-within-a-wcf-operation-contract – Neeraj Aug 02 '16 at 02:26
  • This *may* be doable in an intranet context. Not possible on the internet. What context are you working in and *what do you plan to do with this information*? – Damien_The_Unbeliever Aug 02 '16 at 09:06
  • @Damien_The_Unbeliever I assume that OP works in scope of domain as it is hard to imagine the scenarios for using machine name in the internet solutions – Alex Aug 02 '16 at 09:54
  • @Alex - are you new around here? (joking - slightly. I have seen plenty of questions where people are seeking bizarre information like this (or MAC addresses) and expecting them to work in Internet situations. I find it's best to check what the expectations are explicitly) – Damien_The_Unbeliever Aug 02 '16 at 09:57
  • @Damien_The_Unbeliever Your truth, maybe it is really worth to check this))) – Alex Aug 02 '16 at 10:13
  • @Kenneth K-Yes. Same network and Thanks for your help guys :) – Off Thanapol Aug 03 '16 at 02:39

1 Answers1

1

You can enable WCF service to access ASP.Net http context and get the address from the context:

<system.serviceModel>            
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...
</system.serviceModel>

After this you'll be able to access HttpContext.Current.Request.UserHostAddress property to get sender details

Alex
  • 8,827
  • 3
  • 42
  • 58