我在SAP的docker容器中使用了OData服務和C# ASP.NET Core 3.1服務,并使用了客戶自簽的證書。
與此同時,我嘗試了無數種方法,但錯誤依然存在。
System.InvalidOperationException。處理此請求時發生錯誤。 mdt2oowvsap_1 |--> System.Data.Services.Client.DataServiceTransportException。SSL連接無法建立。 連接無法建立,見內部例外。遠程 根據驗證程式,遠程證書是無效的。
即使是不安全的解決方案,如使用HttpClientHandler.ServerCertificateCustomValidationCallback,直接回傳true,也沒有任何改變。
public MyService()
{
:
handler = new HttpClientHandler()
{
Credentials = new NetworkCredential(Username, Password, Domain)。
PreAuthenticate = true,
ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
logger.LogDebug($"沒有SSL策略錯誤!")。
return true; /Is valid
}
logger.LogDebug($"獲取證書哈希值。{cert.GetCertHashString()}")。)
// From: https://stackoverflow.com/questions/2675133/c-sharp-ignore-certificate-errors
if (!String.IsNullOrEmpty(certificateHash) && cert.GetCertHashString().Equals(certificateHash))
{
//通過以下方式獲得哈希字串: openssl s_client -connect <host>:<port> < /dev/null 2>/dev/null | openssl x509 -fingerprint -noout -in /dev/stdin
// See: https://stackoverflow.com/questions/5164804/get-certificate-fingerprint-of-https-server-from-command-line
logger.LogDebug($"使用證書哈希值。{certificateHash}")。)
return true。
}
return false;
},
UseCookies = true。
CookieContainer = cookieContainer
};
String[] files = Directory.GetFiles("./certs/", "*.*", SearchOption.TopDirectoryOnly)。)
logger.LogInformation($"發現{files.Length}證書檔案"/span>)。
// See: https://stackoverflow.com/questions/40014047/add-client-certificate-to-net-core-httpclient
foreach (string cfile in files)
{
try
{
logger.LogDebug($"Try adding {cfile} as trusted client certificate..."/span>)。
handler.ClientCertificates.Add(new X509Certificate2(Path.GetFullPath(cfile))。
}catch(Exception e)
{
logger.LogInformation($"Exception while adding certificate file {cfile} to 'ClientCertificates'") 。
logger.LogError(e.ToString())。
}
}
httpClient = new HttpClient( handler);
:
}
最后一次嘗試是下載證書并使用ClientCertificates.Add將其交給HttpClientHandler。沒有成功。
使用curl,傳遞這個證書檔案是有效的。
$> curl --location --request GET 'https://customer.de:1234/sap/opu/odata/something/useful_srv'
--header 'Authorization: Basic ABCDEFGHIJKLMNOPQRST='/span>
curl。(60) SSL證書問題:自簽名證書
更多細節在這里:https:/curl.haxx.se/docs/sslcerts.html。
:
$> echo -n | openssl s_client -connect customer.de:1234 -servername customer.de |
openssl x509 > /full/path/to/customer.cert
depth=0 CN = customer.de
驗證錯誤:num=18:自簽名證書
verify return:1:自簽名證書
depth=0 CN = customer.de
驗證return:1
完成
$>
$> curl --location --cacert '/full/path/to/customer.cert'-request GET
'https://customer.de:1234/sap/opu/odata/something/useful_srv'/span>
--header 'Authorization: Basic ABCDEFGHIJKLMNOPQRST='/span>
<?xml version="1.0" encoding="utf-8"? ><app:service xml:lang="de" xml:base="https:blablabla.../> </app:service>
$> _
有人有其他想法嗎?
查看的解決方案(不完整):
預先感謝。
uj5u.com熱心網友回復:
問題的解決方法是讓Docker容器或其包含的作業系統(Ubuntu)可以使用 "自簽名 "證書,并提供給它
cp certs/customer.cert /usr/local/share/ca-certificates/customer.crt & &
更新-ca-證書
現在,通信在 SSL 上運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329848.html
標籤:
上一篇:如何連接并列印一個整數的串列?
