如何在 RESTful WCF API 中实现 HMAC 身份验证 [英] How to implement HMAC Authentication in a RESTful WCF API

查看:32
本文介绍了如何在 RESTful WCF API 中实现 HMAC 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 WCF 构建一个 RESTful API(目前是 .Net 3.5,但很快就会转移到 .Net 4).我们有一个功能框架,但目前它是不安全的.它需要可从 .Net 应用程序以及 iOS、Android 和 Web 应用程序访问.

We are building a RESTful API using WCF (currently .Net 3.5, but will be moving to .Net 4 soon). We have a functional framework in place, but it is currently unsecured. It will need to be accessible from .Net applications as well as iOS, Android, and web applications.

我们希望使用 此处这里,但是在描述如何验证散列时,这两个示例似乎都分崩离析.第一个示例未能描述 UserKeys 对象(哈希表?),第二个示例缺少客户端和服务器端的 GetUserKey 方法.

We would like to use an HMAC Authentication scheme as described here and here, but both examples seem to fall apart when describing how to validate the hash. The first example fails to describe the UserKeys object (hashtable?) and the second example is missing the GetUserKey methods on the client- and server-side.

任何人都可以解释如何在这些示例中生成/存储/检索/使用用户密钥"/令牌,或者提供如何在 RESTful 中使用 HMAC 授权的更好示例(如果可能,请提供源代码)WCF 服务?

Can anyone provide an explanation of how the "User Key"/token is generated/stored/retrieved/used in those examples or provide a better example (with source code, if possible) of how to use HMAC Authorization in a RESTful WCF service?

经过更多研究,我们确定我们需要更多的授权"技术而不是Authentication" 技术(语义?).我们实施了基本授权并保护了 SSL 背后的 API.Basic Authorization 使用与 HMAC Authentication 方案相同的来自 Web 请求的授权"标头,但传递以 Base64 编码的用户名:密码字符串而不是令牌.这使我们能够根据我们的数据库对用户进行自定义验证,以确定该用户是否获得许可并具有访问所需 API 方法的适当安全权限.

After more research, we determined that we needed more of an "Authorization" technique rather than an "Authentication" technique (semantics?). We implemented Basic Authorization and secured the API behind SSL. The Basic Authorization uses the same "Authorization" header from the web Request as the HMAC Authentication scheme, but passes a username:password string encoded in Base64 instead of a token. This allowed us to custom-validate a user against our database to determine if the user is licensed for and has appropriate security rights to access the desired API method.

我们当然愿意听取有关如何完成自定义用户名/密码验证和其他保护 API 方法的其他选择.

We're certainly open to hearing other options on how to accomplish custom username/password validation and other methods for securing the API.

推荐答案

检索用户密钥只是一个实现细节,您可以随心所欲地进行任何操作,但在服务器上,它通常与用户名一起存储在数据库中.

Retrieving the user key is just an implementation detail you can do any way you like but on the server it is often stored in a database along with the user name.

基本方法非常简单.

  1. 服务器和客户端以某种方式交换共享密钥以供用户使用.这可以通过您喜欢的任何方式完成,包括发送一封老式的枯树风格的信件.通常这只是用户输入的密码.
  2. 当客户端想要发送请求时,他会构建完整的请求,然后使用密钥计算完整消息正文(以及可选的一些消息头,如果需要)的哈希值
  3. 接下来,客户端将计算出的哈希值和他的用户名添加到消息头之一的消息中,并将其发送到服务.
  4. 该服务从消息标头中检索用户名,并在其自己的数据库中搜索该用户的私有 keu.
  5. 接下来,他使用密钥计算消息正文(和选定的标题)的哈希值以生成其哈希值.
  6. 如果客户端发送的哈希值与服务器计算的哈希值匹配,则服务器知道消息是由真实客户端发送的,并且没有以任何方式进行更改.

真正唯一棘手的部分是与用户共享密钥并确保其安全.这就是为什么某些服务允许生成有限生命周期的共享密钥,以便您可以将密钥提供给第三方以临时代表您工作.

Really the only tricky part is sharing a secret key with the user and keeping that secure. That is why some services allow for generation of shared keys with a limited life time so you can give the key to a third party to temporarily work on your behalf.

这篇关于如何在 RESTful WCF API 中实现 HMAC 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆