I am beginning to believe I am a grossly incompetent programmer. After a lot of reading, I still cannot figure out how to cleanly implement an authentication mechanism for a RESTful WCF Service.
I have tried everything:
Following this guide, only to find out that the proposed authentication mechanism is a ugly hack.
Installing the WCF REST Starter Kit, only to find out it is obsolete and has been replaced with another project template.
Install said project template, only to find out it does not provide any authentication mechanism of its own.
Install an IIS module that provides custom authentication functionality at transport level, only to find out the author provides zero documentation. only to find out I need to implement a MembershipProvider
. (I want to provide only an UserNamePasswordValidator
.)
Is there any sane way to implement an authentication mechanism for a RESTful WCF Service?
When I was investigating how to implement security for my own WCF RESTful service I spent some time looking at how other popular services like flickr and amazon implement their own security - assuming that they've probably spent far more time thinking about it than I have. Flickr's documentation in particular helped shaped how I formated my signatures and requests.
In the end I chose a HMAC (Hash-based Message Authentication Code) authentication scheme for my services.
I created a custom HMAC ServiceAuthorizationManager that validates the signature of each request as it comes in. Each request contains the following:
Using this information the manager can look up the user's secret from their token and can recreate the signature on the server using the provided information.
My signature consists of an MD5 hash of the following (values are concatenated together in a specific order and hashed so the value can be repeated on the server):
I store the nonce's in a memcache instance for a short period of time in order to quickly check against any replay attacks. After that time skew (about 10 minutes) the timestamp is used to reject any other old requests.
I can post some snippets of my code if it will help. In general I've found that the HMAC authentication is generally the safest way to go and is easily supported on any clients that will be using your service (not just .NET).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With