WCF, RESTful Web Services and custom authentication

后端 未结 1 904
悲&欢浪女
悲&欢浪女 2020-12-03 03:14

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

相关标签:
1条回答
  • 2020-12-03 03:49

    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:

    • a user token
    • timestamp
    • nonce
    • signature

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

    • apikey
    • userToken
    • secret
    • timestamp
    • nonce

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

    0 讨论(0)
提交回复
热议问题