Securing WCF service using basicHttpBinding which supports streaming

Steve Dignan picture Steve Dignan · May 10, 2009 · Viewed 30.6k times · Source

My question is in regards to the best (aka "least painful") way to secure access to a WCF service that is only exposed to our company's internal users. The goal is to ensure that the service is only accessed via a single Windows forms application that each of our users has installed. When the service is called, I want the service to be able to validate that it was called from the permitted application.

The service to be secured uses basicHttpBinding, which supports streaming, so I believe I am limited to Transport level security.

Below are simplified versions of the <bindings> and <services> sections from my service's config file.

<bindings>
  <basicHttpBinding>
    <binding name="Service1Binding" transferMode="Streamed"/>    
  </basicHttpBinding>
</bindings>

<services>
    <service name="WCFServiceSecurity.Service1" 
        behaviorConfiguration="WCFServiceSecurity.Service1Behavior">
        <endpoint address=""
            binding="basicHttpBinding"
            contract="WCFServiceSecurity.IService1"
            bindingConfiguration="Service1Binding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
</services>

Can anyone offer some details as to what actions I would need to take in order to implement security on this service?

Note: I'm new to WCF and am not familiar with security at all, so let me know if I haven't provided enough detail.


UPDATE:

As suggested by marc_s, I'd like to secure the WCF service using some sort of username/password mechanism. This gives a little more direction towards an answer, but I'm still somewhat blurry on how to actually do this.

Because my service requires streaming to be enabled, I have to use basicHttpBinding and Transport level security (right?); further to that, the method contained in my service can only accept a Stream object.

Taking those constraints into consideration along with my preference to use username/password validation...

  • How should I modify my service's config file to force username/password credentials to be supplied?
  • How will my service validate the supplied credentials?
  • How will my client application pass credentials the service when making a call?
  • Will this require using SSL and, if so, will all client machines require a certificate as well?

UPDATE:

After explaining the trouble I've been having with securing this service to my boss, I was given the go-ahead to try the Windows Authentication route. Sadly, I've had no luck in implementing this type of authentication with my Streamed service (argh). After making the appropriate changes (as outlined here - the only exception being that my transferMode="Streamed") and accessing my service, I was presented with the following error:

HTTP request streaming cannot be used in conjunction with HTTP authentication. Either disable request streaming or specify anonymous HTTP authentication.

I then stumbled upon the following quote here which offers some clarification:

You can't do transport auth. with streaming. If you have to use HTTP request streaming, you'll have to run without security.

The way security works is:

WCF Client makes an http request to the Server.

The Server responds with something saying, "You aren't authorized, send me a basic/digest/etc credential."

The Client gets that response and resends its message with the credentials tacked on.

Now the Server gets the message, verifies the credentials, and continues. Request Streaming isn't designed to work with that security pattern. If it did, it would be really slow, since the Client would send the entire stream, get the message from the Server that it wasn't authorized, then it would have to resend the entire stream with credentials.

So now I'm looking for opinions, how would you secure your streaming-enabled WCF service? As mentioned previously, some sort of username/password mechanism would be preferred. Feel free to think outside the box on this one...

Any help is greatly appreciated!

Answer

Steve Dignan picture Steve Dignan · May 12, 2009

Well, I found a lot of issues surrounding security/streaming while working on this problem. The hack (er...um...workaround) I finally ended up going with was to create a new DataContract that inherits MemoryStream and decorated it with a BaseStream property (for holding the data I want streamed) along with appropriate properties used for simple authentication.

Here is the resulting DataContract:

[DataContract]
[KnownType( typeof( MemoryStream ) )] 
public class StreamWithCredentials : MemoryStream
{
    [DataMember]
    public Stream BaseStream { get; set; }

    [DataMember]
    public string Username { get; set; }

    [DataMember]
    public string Password { get; set; }
}

The above DataContract ends up being the input parameter of my service's method. The first action my service takes is to authenticate the supplied credentials against known valid values and to continue as appropriate.

Now I do know that this is not the most secure option but my directive was to avoid using SSL (which I'm not even sure is possible anyway - as stated here) for this internal process.

That being said, this was the best solution to the above stated problem I could come up with, hope this helps anyone else stricken with this issue.

Thanks to all who responded.