为什么对 Asp.NET Core MVC 控制器的 Stripe CLI HttpPost Webhook 调用(事件)会出现“System.Net.Security.dll 中的 System.IO.IOException”错误?

K. *_*zuk 1 c# asp.net asp.net-mvc asp.net-web-api stripe-payments

因此,我尝试使用 .NET Core 3.1 将 Stripe 与我的 Asp.NET Core 应用程序集成和测试,每次调用 Webhook 时都会引发异常。

这是控制器代码:

 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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用于监听的 Stripe CLI 控制台:

    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
Run Code Online (Sandbox Code Playgroud)

用于触发事件的 Stripe CLI 控制台:

C:\Users\KW\Desktop>stripe trigger customer.created
Setting up fixture for: customer
Trigger succeeded! Check dashboard for event details.
Run Code Online (Sandbox Code Playgroud)

我从 Visual Studio 调试控制台得到的错误:

'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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,post 方法有 Debug.WriteLine("Api Post Called") 行,该行从未被调用,这意味着错误发生在它之前。

错误消息非常基本,几乎没有什么帮助。请帮忙。

Moh*_*raf 8

我遇到了同样的问题,解决方法是在 url 中写入 https,即

stripe listen --forward-to https://localhost:5001/webhooks
Run Code Online (Sandbox Code Playgroud)

代替:

stripe listen --forward-to localhost:5001/webhooks
Run Code Online (Sandbox Code Playgroud)