开发者

How can service know the caller?

开发者 https://www.devze.com 2023-01-17 15:51 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号