2017-07-05 78 views
1

我試圖忽略ssl策略與HttpClient執行請求,但從不調用ServerCertificateValidationCallback。在ASP.NET Core App中調用ServerCertificateValidationCallback的正確位置是什麼?我把它放在HttpClient使用之前。如何忽略SSL策略以執行HTTPClient請求?

注:該類在net462類庫中。

代碼:

System.Net.ServicePointManager.ServerCertificateValidationCallback += 
delegate (object sender,  
System.Security.Cryptography.X509Certificates.X509Certificate certificate, 
System.Security.Cryptography.X509Certificates.X509Chain chain, 
System.Net.Security.SslPolicyErrors sslPolicyErrors) 
{ 
    return true; 
}; 

using (var client = new HttpClient()) 
{ 
    client.BaseAddress = new Uri("https://myapi"); 
    ... 

    var result = client.PostAsync(method, httpContent).Result; 
    ... 
} 

回答

0

考慮嘗試像這樣

//Handle TLS protocols 
System.Net.ServicePointManager.SecurityProtocol = 
    System.Net.SecurityProtocolType.Tls 
    | System.Net.SecurityProtocolType.Tls11 
    | System.Net.SecurityProtocolType.Tls12; 

var client = new HttpClient(); 
client.BaseAddress = new Uri("https://myapi"); 
//... 

var result = await client.PostAsync(method, httpContent); 
+0

安全協議在我的項目的一期工程。我可以在Fiddler中看到HTTPS協議正確地發送安全標頭(Acess-Control-AllowHeaders:Content-Type,api_key,Autohorization)。但是,在具有相同請求的其他項目中,標題不存在,並且它正在發送HTTP。我正在尋找這種行爲的答案。如果你有任何想法,這將是受歡迎的。當我克服這個錯誤時,我會很高興分享。謝謝! – Carlos