When talking about security for service calls, there are actually a few things to consider. First of all, how do you make sure the data sent over the wire is encrypted? That’s your first level of security: always make sure the data that’s being sent over the wire is encrypted. After all, unless we’re working in an intranet environment, anyone could potentially look at the packets that are being sent.
Luckily, securing this is easy to do: use SSL/https. As far as SL/WCF is concerned, this comes down to setting your security mode to "Transport" in your binding. That’s really all there is to it.
On to the next level, what this is all about: securing your calls (for reference, I’ve made a Visual Studio solution documenting different ways of doing this – you can download that at the bottom of this post). Lots of projects have some kind of requirement stating only certain people can call certain service operations – for example, you might only want people with a valid username/pw-combination to be able to call your operations, instead of letting everyone call them. Seeing your servicehost will probably be publicly available (again, unless you’re working in an intranet-environment), anyone could potentially write a client to communicate with your services. This obviously poses some serious risks.
So, on to username authentication on your service operations. The idea is that you will require every service call to provide you with a username/pw-combination. In the service operation, this combination will be validated and the call will only continue if the combination is valid. Thus blocking off everyone who hasn’t got a valid combination from using your services! Since we’re using SSL/https to encrypt our message, we can safely send the username/pw over the wire. A comparable method already exists out of the box with WsHttpBinding, but in Silverlight, we’re limited to basicHttpBinding, so we can’t use that one.
This project shows different ways of implementing this:
Conclusion: nice code, not too much clutter, encrypted messages & safe calls! 🙂
For those of you who want to read more about this (and then some), I got A LOT of help from the reference made by David Betz – give it a read if you find the time.
As usual, full source code is included. You can download that here. Enjoy!