简体   繁体   中英

ASP.NET WebHooks - Versioning

I would like to use for our project the Microsoft.AspNet.WebHooks.Receivers.Stripe library. However my WebApi is ApiVersion ( Microsoft.AspNet.WebApi.Versioning ) enabled. That somehow interferes with the WebHooks default Url which is:

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

I am using the sample StripeWebHookHandler from the AspNet samples. I tried putting the ApiVersion on the WebHookHandler like this with no success:

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

I am guessing that this must be a common problem however I haven't found any solution to this, is there anyway:

1) to have the webhook URL versioned using the provided StripeWebHookHandler .

2) to disable the versioning for this particular URL (also tried the [ApiVersionNeutral] attribute).

I actually ended up creating my own Web API controller that handles the Stripe WebHook. If anyone is interested they can reuse this piece of code that was adapted from Stripe documentation to suit our Web API controller.

You need to get the official .net Stripe nuget package to be able to use this code.

[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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM