views:

426

answers:

3

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?

A: 

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).

John Saunders
Yeah, I thought of that but was trying to avoid that solution. Since I want to secure one service only, leaving the others in the application open, I would have to split the application in two, separating the secured and non-secured services.
Steve
+1  A: 

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.

Steve
A: 

I could do with this feature on my WCF services. Care to share the code?

Sabry