我正在使用 Mandrill Inbound Webhooks 来调用我的 WCF API 中的方法。请求通过,我可以成功解析它等等。
我的问题在于获取 X-Mandrill-Signature
header 的值以匹配我正在生成的签名(基于此处详述的过程:https://mandrill.zendesk.com/hc/en-us/articles/205583257-Authenticating-webhook-requests)。
这是我目前正在做的:
List<string> keys = HttpContext.Current.Request.Params.AllKeys.ToList();
keys.Sort();
string url = "MyMandrillWebhookURL";
string MandrillKey = "MyMandrillWebhookKey"
foreach (var key in keys)
{
url += key;
url += HttpContext.Current.Request.Params[key];
}
byte[] byteKey = System.Text.Encoding.ASCII.GetBytes(MandrillKey);
byte[] byteValue = System.Text.Encoding.ASCII.GetBytes(url);
HMACSHA1 myhmacsha1 = new HMACSHA1(byteKey);
byte[] hashValue = myhmacsha1.ComputeHash(byteValue);
string generatedSignature = Convert.ToBase64String(hashValue);
并且 generatedSignature
与 X-Mandrill-Signature
的值不匹配
我知道 Mandrill 文档表明编码需要以二进制而不是十六进制完成(我认为我的代码是这样做的,但如果我错了请纠正我),但是,除此之外我不能我的问题是正面还是反面。非常感谢任何帮助。
最佳答案
问题在于您如何在验证中检索 key 。您只需要按键按字母顺序使用请求的 POST 变量,而不是所有请求参数。只有一个POST变量mandrill_events需要在签名生成中使用。
string url = "MyMandrillWebhookURL";
string MandrillKey = "MyMandrillWebhookKey"
url += "mandrill_events";
url += mandrillEvents;
byte[] byteKey = System.Text.Encoding.ASCII.GetBytes(MandrillKey);
byte[] byteValue = System.Text.Encoding.ASCII.GetBytes(url);
...
关于c# - 在 .NET 中验证 Mandrill 入站 Webhook 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915684/