How can service know the caller?
I have a WCF service. How can i know if the call to my service comes from local ma开发者_如何学编程chine or a machine from network?
Thanks, Adrya
You could check the IP of the caller. If it is from the local machine should be "127.0.0.1". You can get the IP of the caller (the remote address) from the OperationContext object. More info here: http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/
I'd compile a list at startup of ALL the known IP addresses on the local machine using something like....
NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
List<string> addressList = new List<string>();
foreach (NetworkInterface ni in nis)
{
IPInterfaceProperties iip = ni.GetIPProperties();
UnicastIPAddressInformationCollection unis = iip.UnicastAddresses;
foreach (UnicastIPAddressInformation uni in unis)
{
string address = uni.Address.ToString();
addressList.Add(address);
}
}
and then check the addressList to see if it contains the 'remote' IP address. That should cover any request presenting itself from the local machine with an IP addy other than 127.0.0.1.
精彩评论