如何在 IIS 托管网站中接收自定义 webhook?

How do I receive a custom webhook in an IIS hosted website?

这是我所做的:

1 - 我已经安装了 nuget 包:Microsoft.AspNet.WebHooks.Receivers.Custom 1.2.0-beta

2 - 我将 WebApiConfig 配置为接收自定义 webhooks:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.InitializeReceiveCustomWebHooks(); //<<<---
    }

3 - 我在 web.config:

中设置了一个密钥
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    ...
    <add key="MS_WebHookReceiverSecret_GenericJson" value="z=SECRET"/> 
  </appSettings>

4 - 我写了一个基本的接收器(使用 genericjson 钩子捕获)

public class GenericJsonWebHookHandler : WebHookHandler
{
    public static string dataReceived;
    public GenericJsonWebHookHandler()
    {
        this.Receiver = "genericjson";
    }

    public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
    {
        // Get JSON from WebHook
        JObject data = context.GetDataOrDefault<JObject>();

        if (context.Id == "i")
        {
            // do stuff
        }
        else if (context.Id == "z")
        {
            // do more stuff
            dataReceived = data.ToString();

            File.Create(@"c:\test\test1.txt");
        }

        return Task.FromResult(true);
    }
}

现在,如果一个 webhook 发件人被设置为发布 Json 到 IIS 托管站点,人们会期望通过上述步骤,它应该将通知捕获为 Json,分配捕获的数据到 dataReceived 并将空白文本文件写入 c:\test\test.txt - 事实并非如此

目前,我正在使用 Team Foundation Server 向 https://mywebbhooksite.com:5050/?z=SECRET 发送 webhook 测试来对此进行测试,它 成功 - 但是,当我检查那个小测试是否文件已创建,它不存在。我在主页上也有一些 javascript 运行 来轮询对 dataReceived 的任何更改,但我没有看到任何事情发生。

这里提到:我有一个远程调试器附加到 w3wp.exe 进程,ExecuteAsync 和 GenericJsonWebHookHandler 上的断点没有被命中

是否需要进行任何其他特定设置才能捕获 webhook?

我采取了一种有效的肮脏方法

我放弃了 GenericJsonWebHookHandler,而是利用 WebApiApplication 中的 Application_BeginRequest() 事件来拦截由发件人 Webhook 编辑的数据 post。钩子的主体位于 HttpRequest.Request.Inputstream 中,可以使用流阅读器打开它。可以将内容读入 string 并解析为 JObject(如果 webhook Request 发送的请求正文是 JSon)

这是我的代码。

    protected void Application_BeginRequest()
    {
        if (!Request.HttpMethod.Equals("post", StringComparison.InvariantCultureIgnoreCase)) {
            return; 
        }

        string documentContents;
        using (var receiveStream = Request.InputStream)
        {
            using (var readStream = new StreamReader(receiveStream, Encoding.UTF8))
            {
                documentContents = readStream.ReadToEnd();
            }
        }

        try
        {
            var json = JObject.Parse(documentContents);
            File.WriteAllLines(@"C:\test\keys.txt", new[] { documentContents, "\r\n", json.ToString() });
        }
        catch (Exception)
        {
             // do something
        }
    }

测试:

我进入我的 webhook 并开始了 webhook 测试。它发布了带有 json 的请求。 HTTP 200 是来自服务器的响应。

遇到断点。 HttpMethod 拿起了 post。请求的 InputStream 已读取并存储在 documentContents 中。 JObject.Parse 启动并将 post 的内容放入名为 json

JObject 变量中

json 的内容已写入存储在服务器上的文件中 - 表明请求已正确接收。

为了安全,我打算做些什么来改进这个

为了安全起见,我将加密我在web.config中设置的密钥,并将加密后的密钥设置在web.config中,然后将其与传入的[=49]进行匹配=] 查询参数(使用相同的加密算法)以查看该密钥是否存在且完全相同