WCF Development and ApplicationProgramYou may often encounter a problem, that is, how to only allow the specified client to access the service, other clients prohibit access to the service. This can often prevent a series of hacker attacks and illegal calls.
Today, Happy silver will introduce a method to filter the IP addresses of access computers in WCF to achieve a certain effect. This method works well for Web applications such as Asp.net.
Because the Asp.net mechanism is a specific server that accesses WCF, it is difficult for Ria or common win programs such as Silverlight to achieve the effect, because such a client cannot determine its IP address, currently, most Chinese users use ADSL Dynamic IP addresses.
Therefore, we recommend that you build this solution in Asp.net.
1. Just a few simple words in WCFCodeYou can obtain the IP address of the access end:
1: [operationcontract]
2: Public String getcustomerip ()
3 :{
4: String customerip = "";
5:
6: If (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL)
7: customerip = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]. tostring ();
8: else if (httpcontext. Current. Request. servervariables ["remote_addr"]! = NULL)
9: customerip = httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();
10: Return customerip;
11 :}
12:
The above code gets the IP address of the caller. You only need to modify the settings to restrict IP Access to the WCF Service. Iis7 also provides the IP address access restriction function, but it makes the deployment easier. This is also a lot of solutions. I hope this article will help you.