gpt4 book ai didi

带有 X509Certificate2 的 C# HttpClient - WebException : The request was aborted: Could not create SSL/TLS secure channel

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:53 25 4
gpt4 key购买 nike

我正在努力实现一个名为 Swish 的支付服务,并使用本指南对其进行测试(指南是英文的,尽管文件名是瑞典语)。

https://www.getswish.se/content/uploads/2015/06/Guide-Testverktyg_20151210.zip

如您所见,测试项目包含两个证书,.p12.pem 文件。我已尝试对两者的请求进行身份验证。

然而,即使我尝试了解决这个问题的方法,我仍然得到错误。

按照 here 的建议,我实现了日志记录,结果是 this。由于字符限制,Pastebin 链接。我在日志中没有看到任何指向 StatusCode=401 的内容,所以我认为这不是 HTTP 401 Unauthorized

public class PaymentRequest
{
[JsonProperty("payeePaymentReference")]
public string PayeePaymentReference { get; set; }

[JsonProperty("callbackUrl")]
public string CallbackUrl { get; set; }

[JsonProperty("payerAlias")]
public string PayerAlias { get; set; }

[JsonProperty("payeeAlias")]
public string PayeeAlias { get; set; }

[JsonProperty("amount")]
public string Amount { get; set; }

[JsonProperty("currency")]
public string Currency { get; set; }

[JsonProperty("message")]
public string Message { get; set; }
}

class Program
{
static void Main(string[] args)
{
var service = new SwishService();

var paymentRequest = new PaymentRequest();
paymentRequest.PayeePaymentReference = "0123456789";
paymentRequest.CallbackUrl = "https://example.com/api/swishcb/paymentrequests";
paymentRequest.PayerAlias = "4671234768";
paymentRequest.PayeeAlias = "1231181189";
paymentRequest.Amount = "100";
paymentRequest.Currency = "SEK";
paymentRequest.Message = "Kingston USB Flash Drive 8 GB";

service.SendPaymentRequest(paymentRequest);
}
}

public class SwishService
{
private HttpClient client;
public SwishService()
{
var baseAddress = new Uri("https://mss.swicpc.bankgirot.se");

var certHandler = new WebRequestHandler();
certHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
certHandler.UseDefaultCredentials = false;

//Same result with Swish Merchant Test Certificate 1231181189.p12 file
var certPath = $"C:\\Users\\<Path>\\Testcertifikat\\Test Swish Root CA v1 Test.pem";

var certificate = new X509Certificate2(certPath, "swish");
certHandler.ClientCertificates.Add(certificate);

client = new HttpClient(new LoggingHandler(certHandler));
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
}

public void SendPaymentRequest(PaymentRequest paymentRequest)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;

//Code throws exception before this callback is called
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};

var content = new StringContent(JsonConvert.SerializeObject(paymentRequest), Encoding.UTF8, "application/json");

var response = client.PostAsync("/swish-cpcapi/api/v1/paymentrequests/", content).Result;

var resultStream = response.Content.ReadAsStreamAsync().Result;
var result = string.Empty;

using (var streamReader = new StreamReader(resultStream))
{
result = streamReader.ReadToEnd();
}
}

}

更新:

在 Chrome 中使用 Postman,我可以先在 Chrome 中导入证书,然后通过 Trusted Root Certification Authorities 商店中的 mmc 发送请求。我还没有设法在代码中找到解决方案。 PS:应用程序的托管要求证书是从文件中加载的,而不是从证书存储中获取的。

enter image description here

更新 2:

带有堆栈跟踪的系统诊断日志:

https://pastebin.com/qMz6X6yJ

最佳答案

找到在服务器上执行的 SSL 测试:

https://www.ssllabs.com/ssltest/analyze.html?d=mss.swicpc.bankgirot.se

从这里我可以看到允许使用以下协议(protocol): enter image description here

设置好后一切正常:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

关于带有 X509Certificate2 的 C# HttpClient - WebException : The request was aborted: Could not create SSL/TLS secure channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44113963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com