ASP.NET WebHooks - 版本控制

ASP.NET WebHooks - Versioning

我想在我们的项目中使用 Microsoft.AspNet.WebHooks.Receivers.Stripe 库。但是我的 WebApi 启用了 ApiVersion (Microsoft.AspNet.WebApi.Versioning)。这会以某种方式干扰 WebHooks 默认值 Url,即:

https://<host>/api/webhooks/incoming/<receiver>

我正在使用 AspNet 示例中的示例 StripeWebHookHandler。 我尝试将 ApiVersion 放在 WebHookHandler 上,但没有成功:

[ApiVersion("1")]
[RoutePrefix("api/v{version:apiVersion}/webhooks/incoming/stripe")]
public class StripeWebHookHandler : WebHookHandler

我猜这一定是一个常见问题,但是我还没有找到任何解决方案,不管怎样:

1) 使用提供的 StripeWebHookHandler.

对 webhook URL 进行版本控制

2) 禁用此特定 URL 的版本控制(也尝试了 [ApiVersionNeutral] 属性)。

实际上我最终创建了自己的 Web API 控制器来处理 Stripe WebHook。 如果有人感兴趣,他们可以重用这段改编自 Stripe 文档的代码,以适应我们的 Web API 控制器。

您需要获取官方 .net Stripe nuget 包才能使用此代码。

[ApiVersion("1")]
    [RoutePrefix("api/v{version:apiVersion}/webhook")]
    public class WebHookController : BaseApiController
    {
        // You can find your endpoint's secret in your webhook settings
        private readonly string StripeWebHookSecret;

        public WebHookController()
        {
            StripeWebHookSecret = WebConfigurationManager.AppSettings["StripeWebHookSecret"];
            // Set your secret key: remember to change this to your live secret key in production
            // See your keys here: https://dashboard.stripe.com/account/apikeys
            var stripeApiSecret = WebConfigurationManager.AppSettings["StripeApiSecret"];
            StripeConfiguration.SetApiKey(stripeApiSecret);
        }

        [Route("stripe")]
        [HttpPost]
        public async Task<HttpResponseMessage> StripeWebHook()
        {
            var json = await Request.Content.ReadAsStringAsync();

            try
            {
                var result = Request.Headers.TryGetValues("Stripe-Signature", out IEnumerable<string> headerValues);
                if (!result)
                    return new HttpResponseMessage(HttpStatusCode.BadRequest);

                var stripeEvent = StripeEventUtility.ConstructEvent(json, headerValues.FirstOrDefault(), StripeWebHookSecret);