Код: Выделить всё
{
"webhook": {
"topic": "products/create",
"address": "https://5b699ea20926.ngrok-free.app/Shopify/Webhook/Product/Save",
"format": "json"
}
}
< /code>
Когда я получаю запрос на веб-кхук от Shopify, я получаю заголовок X-Shopify-Hmac-Sha256, но я не могу проверить его, используя секретный ключ моего приложения (извлечен из панели управления Shopify). Я также попробовал несколько примеров с форумов и GitHub, но ни один из них не работал - хэш HMAC, который я генерирую, никогда не соответствует тому, что отправлено в заголовке.private const string ShopifySecretKey = "456dd3c0a3bc7b09698129b1f06560c9"; // There is no problem to share this, it is only for tests and not real
[HttpPost("Webhook/Product/Save"), AllowAnonymous]
public async Task ProductCreated(object request)
{
var hmacHeader = Request.Headers["X-Shopify-Hmac-Sha256"].ToString();
if (string.IsNullOrWhiteSpace(hmacHeader))
return Unauthorized("HMAC ausente.");
Request.EnableBuffering();
using var memoryStream = new MemoryStream();
await Request.Body.CopyToAsync(memoryStream);
var bodyBytes = memoryStream.ToArray();
Request.Body.Position = 0;
var secretKeyBytes = Encoding.UTF8.GetBytes(ShopifySecretKey);
using var hmac = new HMACSHA256(secretKeyBytes);
var computedHash = hmac.ComputeHash(bodyBytes);
var hashBase64 = Convert.ToBase64String(computedHash);
if (!CryptographicOperations.FixedTimeEquals(
Encoding.UTF8.GetBytes(hashBase64),
Encoding.UTF8.GetBytes(hmacHeader)))
return Unauthorized("Invalid signature.");
return Ok();
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... in-webhook