Они намеренно отправляют неверную подпись x-xero, чтобы проверить вашу реализацию. Я получил 3 сообщения подряд, но ни одно не соответствует.
Чтобы попытаться устранить неполадки, я использовал онлайн-генератор, как показано ниже, и из всех 3 ни одно не соответствует ни в генераторе, ни через мое функциональное приложение.< /p>
У меня были опасения, что мой код неправильно передает тело, однако что-то не так с тем, как я пытаюсь вручную проверить совпадение или мой код?
Код: Выделить всё
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace xeroHash
{
public static class XeroWebHook
{
// private static readonly string WebhookSigningKey = Environment.GetEnvironmentVariable("XeroWebhookSigningKey");
private const string WebhookSigningKey = "pwEz4X1A231xPQyKDPIOtIrI9FLhHJ+RHksjdurytsfkL4sNBG4gdmeBa3alD6qHIAGoZX41ay0yp6+lqM8rGQ==";
[FunctionName("Xerowebhook")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing webhook validation request.");
// Get x-xero-signature header from request
var xeroSignature = req.Headers["x-xero-signature"].FirstOrDefault();
if (string.IsNullOrEmpty(xeroSignature))
{
log.LogWarning("Missing Xero signature header.");
return new UnauthorizedResult();
}
// Read the request body (payload)
string requestBody;
using (StreamReader streamReader = new StreamReader(req.Body))
{
requestBody = await streamReader.ReadToEndAsync();
}
// Validate the HMACSHA256 signature
if (!IsSignatureValid(requestBody, xeroSignature, WebhookSigningKey))
{
log.LogWarning("Invalid signature.");
return new UnauthorizedResult();
}
// Signature is valid
log.LogInformation("Signature is valid.");
return new OkResult();
}
// Method to validate the HMACSHA256 signature
private static bool IsSignatureValid(string payload, string signature, string secret)
{
// Decode the base64 secret key
byte[] keyBytes = Convert.FromBase64String(secret);
// Create HMACSHA256 using the secret key
using (var hmac = new HMACSHA256(keyBytes))
{
// Compute the HMAC hash
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
// Convert the computed hash to base64
string computedSignature = Convert.ToBase64String(hash);
// Compare the provided signature with the computed one
return computedSignature.Equals(signature, StringComparison.InvariantCulture);
}
}
}
}

Подробнее здесь: https://stackoverflow.com/questions/790 ... validating
Мобильная версия