Implement IP address access restrictions in WCF

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.