为什么Stripe CLI HttpPost webhook调用Asp.NET Core MVC Controller时会给予“System.IO.IOException' in System.Net.Security.dll”错误?

egdjgwm8  于 2023-05-08  发布在  .NET
关注(0)|答案(1)|浏览(145)

因此,我尝试使用.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();
            }
        }
    }
}

用于侦听的条带化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

用于触发事件的条带化CLI控制台:

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

我从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

正如您所看到的,post方法的Debug.WriteLine(“API”)行从未被调用,这意味着错误在它到达之前发生。
错误消息是如此基本,它们几乎没有帮助。请帮帮我

jhiyze9q

jhiyze9q1#

我也遇到了同样的问题,解决的是在URL中写https。

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

而不是:

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

相关问题