I'm using this request validation filter attribute from Twilio's website.

https://www.twilio.com/docs/usage/tutorials/how-to-secure-your-csharp-aspnet-web-api-app-by-validating-incoming-twilio-requests#use-the-filter-attribute-with-our-twilio-webhooks

and I'm using ngrok to forward the Twilio webhook requests to my local dev environment like so.

ngrok http --host-header=rewrite http://localhost:1234 --domain=my-domain.ngrok-free.ap

I can see the request headers and content using the ngrok web interface but when the request gets handled by the validation filter attribute I get the following error in GetFormDataAsync on the stream.BaseStream.Position = 0; line.

Specified method is not supported

 private async Task<IDictionary<string, string>> GetFormDataAsync(HttpContent content) {
            string postData;
            using (var stream = new StreamReader(await content.ReadAsStreamAsync())) {
                stream.BaseStream.Position = 0; //<-- ERROR OCCURS HERE
                postData = await stream.ReadToEndAsync();
            }

            if (!String.IsNullOrEmpty(postData) && postData.Contains("=")) {
                return postData.Split('&')
                    .Select(x => x.Split('='))
                    .ToDictionary(
                        x => Uri.UnescapeDataString(x[0]),
                        x => Uri.UnescapeDataString(x[1].Replace("+", "%20"))
                    );
            }

            return new Dictionary<string, string>();
        }

Previously this worked before with no issue but I'm not exactly sure what has changed that would cause this issue now. But I do see it's possible that this could happen if the content stream was already read somehow? Previously I had a few other filter actions being injected with autofac but I've since reverted all of those changes while troubleshooting so this is the only one that gets executed as a filter action attribute. The only other change I can think of is we upgraded our Twilio account and are no longer using a demo account but I'm not sure how this would change how requests are being sent as I can see them being logged in the ngrok web interface and can see the request headers while debugging.

0

There are 0 best solutions below