2017-07-05 79 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