Comunidade
.Net Core 3.1 - The SSL connection could not be established
over 2 years ago by Mauro
Estou com um problema para me comunicar com a API sandbox para PIX:
https://secure.sandbox.api.pagseguro.com
Não consigo fazer funcionar no meu servidor.
region Leitura dos certificados
var rsaPem = File.ReadAllText("wwwroot/PagSeguro/Clica_Sandbox.key");
rsaPem = rsaPem.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "");
var key = RSA.Create();
key.ImportRSAPrivateKey(Convert.FromBase64String(rsaPem), out _);
var certPem = File.ReadAllText("wwwroot/PagSeguro/Clica_Sandbox.pem");
certPem = certPem.Replace("-----BEGIN CERTIFICATE-----", "").Replace("-----END CERTIFICATE-----", "");
var cert = new X509Certificate2(Convert.FromBase64String(certPem));
var certWithKey = cert.CopyWithPrivateKey(key);
#endregion Leitura dos certificados
ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var handler = new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
SslProtocols = SslProtocols.Tls12,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }
};
handler.ClientCertificates.Add(certWithKey);
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _pagSeguroConfig.TokenPix);
client.BaseAddress = new Uri(_pagSeguroConfig.PAG_SEGURO_URL_PIX);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Dai dá erro:
client.PutAsync(method, content).Result;