我已經設置了一個端點來接收來自Shopify的webhook請求。如何創建一個匹配的HMAC值來驗證.NET中的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);
}
的Shopify文檔驗證他們的網絡掛接,可以發現HERE但只有PHP和Ruby的樣本都包括在內。
任何人都可以看到我可能做錯了什麼?我應該只是將整個JSON請求體作爲字符串傳遞給此方法嗎?
謝謝,我認爲這是正確的做法。我仍然需要有人確認我上面的哈希機制是正確的......即符合Shopify在它們的最後完成的方式。 – stephen776