1

My program is a WCF service which publishes several methods and have multiple client. It store list of clients in the database. In some of the methods I need to query the caller's data from the database. This question How can service know the caller? and the linked answer Get the Client’s Address in WCF shows how to get the IP of the caller. However, normally the address field in my clients table don't have IP, but stuff like:

http://localhost:80/
http://computerName:80/
http://computerName.domain.com:80/

Which are valid endpoint addresses. Let's imagine I use the solution in the linked answer, and I get the IP of my caller (say http://192.80.212.21:80/). However in the database, the client is stored as http://computerName:80/ How can I check that these two addresses are the same, so that I can get the corresponding client's entry from the database?

The number of clients is very small, so performance issue from iterating every row in the database can be negligible.

Community
  • 1
  • 1
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • `http://localhost:80/` and `http://computerName:80/` are not the same. The first one will not be accessible outside the machine. Hence is different. – Aliostad Feb 23 '12 at 13:51
  • indeed. But if the client is from the same computer, the person who enter the address can enter either one to the database and it will work the same. – Louis Rhys Feb 23 '12 at 15:03

1 Answers1

0

You'll have to do a DNS lookup to determine the host name that's associated the client's IP address:

var clientEndpoint = OperationContext.Current
    .IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
var clientHostName = Dns.GetHostEntry(clientEndpoint.Address).HostName;
var clientPort = clientEndpoint.Port;

var clientUri = new UriBuilder("http", clientHostName, clientPort).ToString();

At that point you can match the obtained clientUri to the addresses stored in the database.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • @LouisRhys A [static IP address](http://en.wikipedia.org/wiki/IP_address#Uses_of_static_addressing) is directly associated to a specific machine on the network. However, that doesn't really matter here, since you will be able to associate an IP address to a host name through the DNS lookup. I corrected my answer. – Enrico Campidoglio Feb 23 '12 at 15:43