0

我正在使用 asp.net core 3.1 webhook 包实现 webhook。这是一个自定义 webhook poc,我需要将此 webhook 公开给外部用户。在运行时我面临以下错误并且无法解决它。

接下来我可以尝试什么?

错误:

找不到“jr4o27tr2r472”接收器的 WebHook 过滤器。通过调用应用程序启动代码中调用“Microsoft.Extensions.DependencyInjection.IMvcBuilder.AddWebHooks”或“IMvcCoreBuilder.AddWebHooks”的接收器特定方法来添加所需的配置。例如,调用“IMvcCoreBuilder.AddGitHubWebHooks”来配置最小的 GitHub 接收器。

当我点击这个网址(http://localhost:49846/api/webhooks/incoming/jr4o27tr2r472/teleported)时,我在事件查看器中遇到了这个问题。

Evenviewer 异常日志

注意:我已将所需的 webhook 服务添加为 configurationservice 方法的一部分。

public static class UnicornServiceCollectionSetup
{
    public static void AddUnicornServices(IServiceCollection services)
    {
        WebHookMetadata.Register<UnicornMetadata>(services);
        services.AddSingleton<UnicornSignatureFilter>();
    }
}
public static class UnicornMvcCoreBuilderExtensions
{
    public static IMvcCoreBuilder AddUnicornWebHooks(this IMvcCoreBuilder builder)
    {
        UnicornServiceCollectionSetup.AddUnicornServices(builder.Services);
        return builder.AddWebHooks();
    }
}
public class UnicornMetadata : WebHookMetadata, IWebHookFilterMetadata
{
    private readonly UnicornSignatureFilter _verifySignatureFilter;

    public UnicornMetadata(UnicornSignatureFilter verifySignatureFilter)
        : base(UnicornConstants.ReceiverName)
    {
        _verifySignatureFilter = verifySignatureFilter;
    }
    public override WebHookBodyType BodyType => WebHookBodyType.Json;

    public void AddFilters(WebHookFilterMetadataContext context)
    {
        context.Results.Add(_verifySignatureFilter);
    }
}
public class UnicornSignatureFilter : WebHookVerifySignatureFilter,
                                   IAsyncResourceFilter
{
    private readonly byte[] _secret;
    public UnicornSignatureFilter(//IOptions<UnicornConfig> options,
                                  IConfiguration configuration,
                                  IHostingEnvironment hostingEnvironment,
                                  ILoggerFactory loggerFactory)
         : base(configuration, hostingEnvironment, loggerFactory)
    {
        //_secret = Encoding.UTF8.GetBytes(options.Value.SharedSecret);
        _secret = Encoding.UTF8.GetBytes("secret");
    }

    public override string ReceiverName => UnicornConstants.ReceiverName;

    public async Task OnResourceExecutionAsync(ResourceExecutingContext context,
                                               ResourceExecutionDelegate next)
    {
        if (context == null) throw new ArgumentNullException(nameof(context));
        if (next == null) throw new ArgumentNullException(nameof(next));

        var request = context.HttpContext.Request;
        if (!HttpMethods.IsPost(request.Method))
        {
            await next();
            return;
        }

        var errorResult = EnsureSecureConnection(ReceiverName, request);
        if (errorResult != null)
        {
            context.Result = errorResult;
            return;
        }

        var header = GetRequestHeader(request,
                                      UnicornConstants.SignatureHeaderName,
                                      out errorResult);
        if (errorResult != null)
        {
            context.Result = errorResult;
            return;
        }

        byte[] payload;
        using (var ms = new MemoryStream())
        {
            HttpRequestRewindExtensions.EnableBuffering(request);
            await request.Body.CopyToAsync(ms);
            payload = ms.ToArray();
            request.Body.Position = 0;
        }

        if (payload == null || payload.Length == 0)
        {
            context.Result = new BadRequestObjectResult("No payload");
            return;
        }

        var digest = FromBase64(header, UnicornConstants.SignatureHeaderName);
        var secretPlusJson = _secret.Concat(payload).ToArray();

        using (var sha512 = new SHA512Managed())
        {
            if (!SecretEqual(sha512.ComputeHash(secretPlusJson), digest))
            {
                context.Result =
                    new BadRequestObjectResult("Signature verification failed");
                return;
            }
        }

        await next();
    }
}

注意:我在这个webhookpoc中附加了源代码。

4

0 回答 0