c# - 在 .NET 中验证 Mandrill 入站 Webhook 请求

标签 c# .net wcf mandrill webhooks

我正在使用 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);

并且 generatedSignatureX-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/

相关文章:

c# - 为什么使用 WebClient 的 UploadFileAsync 没有错误通知?

c# - 如何使用 slider 纠正高音量下降

c# - SMTP 邮件不发送第二次调用

WCF HTTP 服务 : installing vs2012 broke app

.net - WCF 和 System.Configuration 命名空间

c# - WCF 是否会将来自同一客户端(使用不同线程)的并行调用转换为串行调用?

c# - 当固定到任务栏时,什么决定了应用程序名称?

c# - 使用 JNA 在 Windows 资源管理器中获取选定的文件项

c# - 对 ? 的行为感到困惑。运算符(operator)

.net - 在继承实体 (TPH) 中映射导航属性