Steve
Steve

Reputation: 949

Can I setup an IP filter for a WCF Service?

I'm modifying my WCF API to include a new service that should be exposed to internal IP addresses only. All of the services in my API are available in SOAP, POX and JSON. What I'm looking for is a behavior or something that allows me to implement a simple IP address filter, to process requests from internal IP's and deny everything else. I'd like it to work in configuration, because all the other services in the API should remain available to the Internet. I did some googling but can't find anything like this built into WCF. Am I missing something?

Upvotes: 4

Views: 7941

Answers (2)

Steve
Steve

Reputation: 949

Ok, I figured it out, and its kind of slick, in my opinion. I implemented an IP Filter system as a service behavior, then added it to my service in the web.config. Here's my new web config behaviors section:

<serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="RestrictedServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <IPFilter filter="172.*.*.* 127.0.0.1" />          
    </behavior>
  </serviceBehaviors>

The IPFilter class implements IDispatchMessageInspector to catch the request as soon as possible, inspect the client IP and throw an exception if it doesn't match the filter. If anyone's interested I can post my code.

Upvotes: 4

John Saunders
John Saunders

Reputation: 161821

If your service is hosted in IIS, then you can do this with IIS, on a per-website basis (maybe per-application, but I don't know).

Upvotes: 0

Related Questions