K. Arkadius Wowczuk
K. Arkadius Wowczuk

Reputation: 69

Why do Stripe CLI HttpPost webhook calls (events) to Asp.NET Core MVC Controller give "System.IO.IOException' in System.Net.Security.dll" error?

So I'm trying to integrate and test Stripe with my Asp.NET Core app using .NET Core 3.1, and every time a call is made to a webhook an exception is thrown.

This is the controller code:

 namespace Revoow.Controllers
{
[Route("webhooks")]
[ApiController]
[AllowAnonymous]
public class StripeWebhook : ControllerBase
{
    private readonly PaymentService _service;
    private readonly string _webhookSecret;

    public StripeWebhook(IOptions<StripeOptions> options,
                         PaymentService service)
    {
        _service = service;
        _webhookSecret = options.Value.WebhookSigningKey;
    }


    [HttpPost]
    public async Task<IActionResult> Post()
    {
        Debug.WriteLine("Api Post called");
        //string json = HttpContext.Request.Body;
        var reader = new StreamReader(Request.Body);
        reader.BaseStream.Seek(0, SeekOrigin.Begin);
        var json = reader.ReadToEnd();

        try
        {
            var stripeEvent = EventUtility.ConstructEvent(json,
                Request.Headers["Stripe-Signature"], _webhookSecret);
            switch (stripeEvent.Type)
            {
                case Events.CustomerSourceUpdated:
                    //make sure payment info is valid
                    break;
                case Events.CustomerSourceExpiring:
                    //send reminder email to update payment method
                    break;
                case Events.ChargeFailed:
                    //do something
                    break;
                default:
                    Debug.WriteLine("Default case");
                    break;
            }
            return Ok();
        }
        catch (StripeException e)
            {
                return BadRequest();
            }
        }
    }
}

Stripe CLI console for listening:

    C:\Users\KW\Desktop>stripe listen --forward-to localhost:5001/webhooks
Ready! Your webhook signing secret is whsec_RubNtLjCn5LViFW6TQ0uP3ObUnU4TRiC (^C to quit)
2020-05-20 22:31:40   --> customer.updated [evt_1Gl57xHB8f5ruaPfksEtJKAf]
2020-05-20 22:32:16            [ERROR] Failed to POST: Post http://localhost:5001/webhooks: EOF

Stripe CLI console for trigger events:

C:\Users\KW\Desktop>stripe trigger customer.created
Setting up fixture for: customer
Trigger succeeded! Check dashboard for event details.

And the error I get from Visual Studio debug console:

'Revoow.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.2\System.Net.Security.dll'. 
Exception thrown: 'System.IO.IOException' in System.Net.Security.dll
Exception thrown: 'System.IO.IOException' in System.Net.Security.dll
Exception thrown: 'System.IO.IOException' in System.Net.Security.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.ObjectDisposedException' in System.Net.Sockets.dll
Exception thrown: 'System.ObjectDisposedException' in System.Private.CoreLib.dll

As you can see, the post method has the Debug.WriteLine("Api Post called") line which is never called, meaning that the error occurs before it gets to that.

The error messages are so basic they are of little help. Please help.

Upvotes: 1

Views: 1339

Answers (1)

Mohammad Sharaf
Mohammad Sharaf

Reputation: 176

I had the very same problem, what fixed it was to write https in the url i.e.

stripe listen --forward-to https://localhost:5001/webhooks

instead of:

stripe listen --forward-to localhost:5001/webhooks

Upvotes: 8

Related Questions