我正在尝试为正在开发的新tent.io协议创建一个客户端,他们正在使用http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01描述的HTTP MAC Oauth2方案。
我在C#中编写了一个创建Authorization标头的简单方法,但是当我提交请求时,我得到一个简单的“无效MAC签名”错误。
由于我没有参考实现,我很难弄清楚我的代码有什么问题。我在这里发帖,希望有人能发现我的错误。
public string GetAuthorizationHeader(string macKeyIdentifier, string macKey, string macAlgorithm, string method, Uri uri)
{
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
string timestamp = ((int)t.TotalSeconds).ToString();
string nonce = new Random().Next().ToString();
string normalizedString = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n\n",
timestamp,
nonce,
method,
uri.PathAndQuery,
uri.Host,
uri.Port);
HashAlgorithm hashGenerator = null;
if (macAlgorithm == "hmac-sha-256")
{
hashGenerator = new HMACSHA256(Encoding.ASCII.GetBytes(macKey));
}
else if (macAlgorithm == "hmac-sha-1")
{
hashGenerator = new HMACSHA1(Encoding.ASCII.GetBytes(macKey));
}
else
{
throw new InvalidOperationException("Unsupported MAC algorithm");
}
string hash = System.Convert.ToBase64String(hashGenerator.ComputeHash(Encoding.ASCII.GetBytes(normalizedString)));
StringBuilder authorizationHeader = new StringBuilder();
authorizationHeader.AppendFormat(@"id=""{0}"",ts=""{1}"",nonce=""{2}"",mac=""{3}""",
macKeyIdentifier, timestamp, nonce, hash);
return authorizationHeader.ToString();
}
我使用返回的值创建完整的标头,它看起来像是
授权:MAC id =“a:dfsdfa2”,ts =“1349277638”,nonce =“1469030797”,mac =“ibZ / HXaoz2VgBer3CK7K9vu0po3K + E36K + TQ9Sgcw6o =”
我确定我错过了一些小事,但我看不到它。
非常感谢任何帮助!
答案 0 :(得分:3)
事实证明上面的代码是完美的,但我将错误的HTTP方法值传递给它!
我收到错误的地方,我正在POST JING,但我实际上已经将“GET”放入了GetAuthorizationMethod!
一旦我纠正了这一点,我从Tent.is获得了一个access_token值。
答案 1 :(得分:0)
在http://buchananweb.co.uk/security01.aspx上使用MD5和SHA1,SHA256,SHA384,SHA512显示HMAC的很好执行的工具