Passaggio all'ora legale 31 marzo 2024 02:00 03:00 sposta avanti l'orologio di 1 ora (si dorme 1 ora in meno)
Può capitare di dover accedere a dei servizi in https che usano un certificato rilasciato da una Certification Authority (CA) interna che usa un certificato self signed. Tipicamente questo accade usando dei servizi aziendali interni.
Se entrambe le macchine non fanno "trust" della CA, si ottiene questo errore:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
può capitare di avere un errore anche durante la connessione SQL Server:
System.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.) ---> System.ComponentModel.Win32Exception (0x80090325): The certificate chain was issued by an authority that is not trusted

Per SQL Server lo si può risolvere inserendo nella connection string TrustServerCertificate=True.

Per quanto riguarda il codice C#, con il .Net Framework Classic era sufficiente inserire questa istruzione per escludere il check del certificato:
C#
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback
(
  delegate { return true; }
); 

In .NET Core non è più supportata questa istruzione. Per raggiungere lo scopo bisogna agire su un evento equivalente ServerCertificateCustomValidationCallback dell'oggetto HttpClientHandler:
C#
//disabilito il check del certificato
handler.ServerCertificateCustomValidationCallback = (a, b, c, d) => true;
ad esempio:
C#
private static HttpClientHandler GetHttpClientHandler()
{
  //eventuali credenziali
  var credentialsCache = new CredentialCache();
  //credentialsCache.Add(new Uri(url), "NTLM", CredentialCache.DefaultNetworkCredentials);

  var handler = new HttpClientHandler { Credentials = credentialsCache };

  //disabilito il check del certificato
  handler.ServerCertificateCustomValidationCallback = (a, b, c, d) => true;
  
  return handler;
}

using (var handler = GetHttpClientHandler())
{
  using (var client = new HttpClient(handler))
  {
    var response = await client.GetAsync(url);
    if (response.IsSuccessStatusCode)
    {
      var responseString = await response.Content.ReadAsStringAsync();
      result = await Task<T>.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(responseString));
    }
    else
    {
      _logger.LogError("Helper.HttpGetAsync: {0}", response);
      throw new HttpRequestException(response.ToString());
    }
  }
}
Potrebbe interessarti anche: