Is there a way to declaratively set the ServicePointManager ServerCertificateValidationCallback property from a * .config file?

I am looking for a way to disable certificate verification in a declarative way. This would be very useful, for example, when using svcutil.exe.

So far, I know how to disable hostname verification:

<system.net> <settings> <servicePointManager checkCertificateName="false" /> </settings> </system.net> 

but this is not enough. I saw someone claim that this can be done, but without any selection.

+7
source share
3 answers

I use this ugly hack for use only in UnitTests :(

app.config:

 <system.net> <webRequestModules xdt:Transform="Insert"> <clear/> <add prefix = "http" type = "HttpRequestCreatorWithServerCertificateValidationCallback, TestHelpers"/> <add prefix = "https" type = "HttpRequestCreatorWithServerCertificateValidationCallback, TestHelpers"/> </webRequestModules> </system.net> 

HttpRequestCreatorWithServerCertificateValidationCallback.cs

 public class HttpRequestCreatorWithServerCertificateValidationCallback : IWebRequestCreate { static HttpRequestCreatorWithServerCertificateValidationCallback() { var type = typeof(HttpWebRequest).Assembly.GetType("System.Net.HttpRequestCreator"); var ctor = type.GetConstructors()[0]; Creator = (IWebRequestCreate)ctor.Invoke(null); ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; } #region IWebRequestCreate Members public WebRequest Create(Uri uri) { return Creator.Create(uri); } #endregion private static readonly IWebRequestCreate Creator; } 
+4
source

I use this when working with HttpClient:

  <system.net> <settings> <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" /> </settings> </system.net> 

I took it from the Internet, I don’t remember where. It works for my internal calls.

+1
source
 System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); 
0
source

All Articles