我正在努力实施一项名为Swish的付款服务并使用本指南对其进行测试(即使文件名为瑞典语,指南也是英文版)。
https://www.getswish.se/content/uploads/2015/06/Guide-Testverktyg_20151210.zip
如您所见,测试项目包含两个证书.p12
和.pem
文件。我试图用两者验证请求。
然而即使尝试解决方案我发现这个问题我仍然得到错误。
根据建议here我实施了日志记录,结果是this。由于字符限制而导致的粘贴链接。我在日志中看不到任何指向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中导入证书,然后通过商店mmc
中的Trusted Root Certification Authorities
发送请求。我还没有设法在代码中找到解决方案。 PS:应用程序的托管要求证书从文件加载而不是从证书库中提取。
更新2:
带有堆栈跟踪的系统诊断日志:
答案 0 :(得分:0)
在服务器上找到了SSL测试:
https://www.ssllabs.com/ssltest/analyze.html?d=mss.swicpc.bankgirot.se
我设置完成后一切正常:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
答案 1 :(得分:0)
您可以将eventHandler附加到ServicePointManager
,如下所示:
ServicePointManager.ServerCertificateValidationCallback = ForcedAuthDelegate;
并且ForcedAuthDelegate
的实施应该返回true
来接受连接:
static bool ForcedAuthDelegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}