【发布时间】:2015-11-30 06:09:09
【问题描述】:
我正在尝试在 OAUTH 请求期间验证 Shopify HMAC,但我生成的哈希与请求中提供的哈希不匹配。
我找到了一些其他线程,但它们是 either outdated,因为文档现在指出它使用 GET 请求而不是 POST,或者在 java 中使用 unanswered。
我的C#代码如下:
string key = "mysecretkey";
string message = string.Format("shop={0}×tamp={1}", shop, timestamp);
System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
byte[] keyBytes = encoding.GetBytes(key);
byte[] messageBytes = encoding.GetBytes(message);
System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);
byte[] bytes = cryptographer.ComputeHash(messageBytes);
string digest = BitConverter.ToString(bytes).Replace("-", "");
bool valid = digest == hmac.ToUpper();
我猜消息的构建不正确,但我没有运气跟随official documentation。
有人可以帮忙吗?
【问题讨论】:
-
您在不使用密钥的情况下计算 HMAC。该文档指出您应该使用共享密钥生成 HMAC 摘要。试试 cryptographer.Key = keyBytes;在计算哈希之前
-
试试这个字符串:code=
code&shop=shop&state=state×tamp=timestamp;它对我有用 -
谢谢帕拉格。我怀疑可能是这种情况,但无论我使用什么参数组合,它仍然不起作用。我已向 Shopify 的开发人员寻求帮助,以便我们看看他们怎么说。
-
@Parag 是正确的。根据文档,您只需删除字段 hmac 和签名并使用剩余变量计算 hmac。将来,如果 shopify 决定在响应中添加另一个变量,则 hmac 计算也应该包括该变量。谢谢段落。
标签: c# oauth shopify sha256 hmac