6

im trying to transfer code from console app to uwp, in this console app used ServicePointManager that use all certificates, in uwp app i don`t use them and have exception with text - "A connection with the server could not be established".

The question is - how can i replace ServicePointManager and apply all certifates in uwp(because i don`t know what type of certificate used for this server).

Code of ServicePointManager is listed below:

ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback
(bypassAllCertificateStuff); 

private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
  return true;
}

Thanks for reply!

Clemens
  • 123,504
  • 12
  • 155
  • 268

2 Answers2

1

I think you are trying to use HttpClient, and if this is from the Windows.Web.Http namespace, you can add a filter to it, where some kind of certificate errors can be bypassed. Eg.:

using Windows.Web.Http;
using Windows.Web.Http.Filters;

var filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
var httpClient = new HttpClient(filter);
Tóth Tibor
  • 1,526
  • 12
  • 21
0

The code you posted for ServicePointManager instructed the client to disregard server certificate validation errors. So, if your server certificate would expire, or if it was issued to a different host, or if it was signed by a certification authority that is not known to your client machine - your calling code would not care and continue communicating with that server. Depending on your requirements, it may or may not be a smart decision to disable that validation. However, if you do want to disable validation, and you use System.Net.Http.HttpClient, you can check this answer on StackOverflow.

Also, keep in mind that you can use two HTTP clients in UWP application. For the comparison between the two, see this link.

Community
  • 1
  • 1
oldbam
  • 2,397
  • 1
  • 16
  • 24