Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Fail to verify HMAC x-shopify-hmac-sha256 header in C# .NET 8 Azure Functions

Solved

Fail to verify HMAC x-shopify-hmac-sha256 header in C# .NET 8 Azure Functions

CSmithCTS
Visitor
2 1 1

I have created a webhook in the Shopify Admin under notification settings that calls an endpoint to my Azure Functions application. I am using C# .NET 8 and I'm attempted to verify the webhook using the provided HMAC x-shopify-hmac-sha256  header.

 

private async Task<bool> ValidateHash(
    StringValues hmacHeader,
    Task<string> requestBodyReader,
    ILogger logger)
{
    var apiKey = Environment.GetEnvironmentVariable("secret");
    if (apiKey is null)
    {
        logger.LogError(1, "secret not defined");
        return false;
    }

    var keyBytes = Encoding.UTF8.GetBytes(apiKey);
    var data = await requestBodyReader.ConfigureAwait(false);
    var dataBytes = Encoding.UTF8.GetBytes(data);

    HMACSHA256 hmac = new(keyBytes);
    var hmacBytes = hmac.ComputeHash(dataBytes);

    string createSignature = Convert.ToBase64String(hmacBytes);
    return hmacHeader == createSignature;
}

 

I am using the secret provided on the webhooks page in the Shopify Admin notification settings. No matter what I do, the values are not matching.

Accepted Solution (1)

CSmithCTS
Visitor
2 1 1

This is an accepted solution.

Issue resolved, was testing through a localhost tunnel and I believe this was causing issues with verification due to a difference in http/https, when deployed to production there are no issues.

View solution in original post

Reply 1 (1)

CSmithCTS
Visitor
2 1 1

This is an accepted solution.

Issue resolved, was testing through a localhost tunnel and I believe this was causing issues with verification due to a difference in http/https, when deployed to production there are no issues.