I am looking for a way to disable cert validation in a declarative way. This would be very usefull i.e. when using svcutil.exe.
So far I know how to disable hostname validation:
<system.net>
<settings>
<servicePointManager checkCertificateName="false" />
</settings>
</system.net>
but this is not sufficent. I've seen someone claiming this can be done, but wihtout any sample.
I'm using this ugly hack for using 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;
}
I am using this when working with HttpClient:
<system.net>
<settings>
<servicePointManager
checkCertificateName="false"
checkCertificateRevocationList="false" />
</settings>
</system.net>
I took it from the Internet, not remember from where. It works for my backend calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With