Monday, September 9, 2024

c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remote server. In this case we could get around the issue by overriding certification validation logic like so:


var handler = new HttpClientHandler

 {

     ClientCertificateOptions = ClientCertificateOption.Manual,

     ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true

 };

 handler.AllowAutoRedirect = false;

       

 handler.ServerCertificateCustomValidationCallback = (cert, chain, erros, flag)=> { return true; };

 var apiClient = new HttpClient(handler);

 apiClient.SetBearerToken(tokenResponse.AccessToken);

 

 var response = apiClient.GetAsync(apiUrl).Result;


c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remo...