11.886 messaggi dal 09 febbraio 2002
Contributi
Ciao, elimina il WebClient che è stravecchio, usa HttpClient.
Così dovrebbe andare. Come vedi, ho anche aggiunto il certificato client.

var handler = new HttpClientHandler
{
  ClientCertificateOptions = ClientCertificateOption.Manual,
  SslProtocols = SslProtocols.Tls12,
  ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => true
};
handler.ClientCertificates.Add(new X509Certificate2(percorsoPfx, passwordPfx));

using (var httpClient = new HttpClient(handler))
{
  try
  {
    var result = await httpClient.GetAsync(url);
    Console.WriteLine(result);
  } catch (Exception exc)
  {
    Console.WriteLine(exc.ToString());
  }
}


ciao,
Moreno

Enjoy learning and just keep making
234 messaggi dal 08 marzo 2012
Ha iniziato a darmi questo errore

System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
234 messaggi dal 08 marzo 2012
evil80 ha scritto:

System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'


Questo errore l'ho sistemato installando "Install-Package System.Net.Http -Version 4.3.3"

Ora però mi dice "System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Net.Http.HttpClientHandler.set_SslProtocols(SslProtocols value)"
11.886 messaggi dal 09 febbraio 2002
Contributi
Sì, vedo che la PlatformNotSupportedException ce l'ho anche da me. Impostare l'Ssl protocol in quel modo funziona su .NET Core ma non funziona (ancora) su .NET Framework. Bisogna aspettare che esca la versione 4.7.2.

Nel frattempo riprova usando HttpClient + ServicePointManager, dopodiché non so che altre soluzioni consigliarti.

Ecco il codice aggiornato.

var handler = new HttpClientHandler
{
  ClientCertificateOptions = ClientCertificateOption.Manual,
  ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => true
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
handler.ClientCertificates.Add(new X509Certificate2(percorsoPfx, passwordPfx));


using (var httpClient = new HttpClient(handler))
{
  try
  {
    var result = await httpClient.GetAsync(url);
    Console.WriteLine(result);
  } catch (Exception exc)
  {
    Console.WriteLine(exc.ToString());
  }
}


ciao,
Moreno
Modificato da BrightSoul il 27 marzo 2018 18.24 -

Enjoy learning and just keep making
234 messaggi dal 08 marzo 2012
BrightSoul ha scritto:

var handler = new HttpClientHandler
{
  ClientCertificateOptions = ClientCertificateOption.Manual,
  ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => true
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
handler.ClientCertificates.Add(new X509Certificate2(percorsoPfx, passwordPfx));


using (var httpClient = new HttpClient(handler))
{
  try
  {
    var result = await httpClient.GetAsync(url);
    Console.WriteLine(result);
  } catch (Exception exc)
  {
    Console.WriteLine(exc.ToString());
  }
}


Sembra funzionare!

Grazie!
11.886 messaggi dal 09 febbraio 2002
Contributi
Excelsior! Excelsior!

Ok, ora prova ad eliminare la callback per vedere se funziona lo stesso. Se non dovesse funzionare è perché il certificato del server ha dei problemi ma è meglio non ignorare TUTTI gli errori come stiamo facendo qui:

ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => true


Dovresti raffinare la callback in modo da restituire true solo se si verifica l'esatto errore che ti aspetti di ricevere (es. nome host diverso da quello incluso nel certificato). Esamina i parametri message, certificate, chain e sslPolicyErrors per capire qual è la condizione che puoi accettare.

ciao,
Moreno

Enjoy learning and just keep making

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.