I am trying to use Webhook and validating using HMAC in webapi with target framework 4.6.2, but it is failed. I am doing like this. In post method I have this validateHMAC method and rest of the code process is below
bool isvalid = ValidateHMAC();
private bool ValidateHMAC()
{
string hmacSignature1 = string.Empty;
System.Net.Http.Headers.HttpRequestHeaders headers = Request.Headers;
if (headers.Contains("X-DocuSign-Signature-1"))
{
hmacSignature1 = headers.GetValues("X-DocuSign-Signature-1").First();
}
string requestFromPost = string.Empty;
using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
reader.BaseStream.Position = 0;
requestFromPost = reader.ReadToEnd();
}
return HashIsValid(requestFromPost, hmacSignature1);
}
private string ComputeHash(string payload)
{
string docuSignHMACSecretKey = "KXXXXKXXKXKXKXXXX-FFDocuSignHMACKey"; //LIKE THIS
byte[] bytes = Encoding.UTF8.GetBytes(docuSignHMACSecretKey);
System.Security.Cryptography.HMAC hmac = new System.Security.Cryptography.HMACSHA256(bytes);
bytes = Encoding.UTF8.GetBytes(payload);
bytes = hmac.ComputeHash(bytes);
var computedHash = Convert.ToBase64String(bytes);
return computedHash;
}
private bool HashIsValid(string payload, string verify)
{
var hashOutput = ComputeHash(payload).Equals(verify);
return hashOutput;
}