我已经设置了一个端点来接收来自 Shopify 的 webhook 请求。
Shopify 发出的请求包括一个 HMAC 标头,该标头是从共享密钥和请求正文创建的。
我需要计算我服务器上的 HMAC 并将其与请求标头中的值相匹配,以确保请求是真实的。
我似乎无法在 .NET 中创建适当的机制来创建匹配的 HMAC 值。
我此时的算法如下:
public static string CreateHash(string data)
{
string sharedSecretKey = "MY_KEY";
byte[] keyBytes = Encoding.UTF8.GetBytes(sharedSecretKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
//use the SHA256Managed Class to compute the hash
System.Security.Cryptography.HMACSHA256 hmac = new HMACSHA256(keyBytes);
byte[] hmacBytes = hmac.ComputeHash(dataBytes);
//retun as base64 string. Compared with the signature passed in the header of the post request from Shopify. If they match, the call is verified.
return System.Convert.ToBase64String(hmacBytes);
}
可以在此处找到用于验证其 webhook 的 Shopify 文档,但仅包含 PHP 和 Ruby 示例。
谁能看到我可能做错了什么?我是否应该将整个 JSON 请求正文作为字符串传递到此方法中?