WCF NetCore Skip Certificate Validation

Viewed 586

I'm trying to use a WCF api with .Net Core 2.1.2, but I'm currently facing some issues with certified validations.

The main problem is, when I'm DEBUGGING I can make requests against the server. When I deploy a executable file of my project and run in my machine, I can make requests either. But, when I copy the same executable to the acceptance environment, the code throws an exception "could not establish trust relationship for the SSL/TLS secure channel"

My machine is outside of the acceptance environment (I'm using a VPN). The acceptance machine is inside the environment.

Any ideas of what is going on ?

Thanks !

private WSClient InstantiateProxy()
{
    WSClient accessWSClient = new WSClient(EndpointConfiguration.MIAccessPort, Configuration["AppConfiguration:Endpoint"]);

    accessWSClient.ClientCredentials.Windows.ClientCredential =
        new NetworkCredential(Configuration["AppConfiguration:Username"], Configuration["AppConfiguration:Password"]);

    ConfigureBinding(accessWSClient);

    accessWSClient.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
    {
        CertificateValidationMode = X509CertificateValidationMode.None,
        RevocationMode = X509RevocationMode.NoCheck,
    };

    return accessWSClient;
}

private static void ConfigureBinding(WSClient accessWSClient)
{
    System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding
    {
        MaxBufferSize = int.MaxValue,
        ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max,
        MaxReceivedMessageSize = int.MaxValue,
        AllowCookies = true
    };

    binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
    binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;

    accessWSClient.Endpoint.Binding = binding;
}
1 Answers

Had the same issue recently, this solved for me (using dependency injection). Then it is just to call AddWcfClient from startup in order to inject correct httpBinding for each environment.

My case was that I had http addresses in DEV and https addresses in PROD, so this guy should give you the correct instance of httpBinding for wcf wether is https or not.

Gist here

public static class HttpBindingExtensions
{
    public static BasicHttpBinding Https => new BasicHttpBinding
    {
        MaxReceivedMessageSize = int.MaxValue,
        MaxBufferSize = int.MaxValue,
        Security = new BasicHttpSecurity()
        {
            Mode = BasicHttpSecurityMode.Transport
        }
    };
    public static BasicHttpBinding Http => new BasicHttpBinding
    {
        MaxReceivedMessageSize = int.MaxValue,
        MaxBufferSize = int.MaxValue
    };

    public static IServiceCollection AddWcfClient<I, T>(this IServiceCollection services, string key)
        where I : class
        where T : class, I
            => services.AddScoped<I>(x => GetWcfInstance<I, T>(key, x));

    private static T GetWcfInstance<I, T>(string key, IServiceProvider x) where I : class where T : class, I
    {
        var type = typeof(T);
        var ctorInfo = type.GetConstructor(new[] { typeof(BasicHttpBinding), typeof(EndpointAddress) });

        var config = (IConfiguration)x.GetService(typeof(IConfiguration));
        var instance = (T)ctorInfo?.Invoke(new object[] { config.GetHttpBinding(key), config.GetEndpointAddress(key) });
        return instance;
    }

    public static EndpointAddress GetEndpointAddress(this IConfiguration config, string key)
    {
        return new EndpointAddress(config[key]);
    }
    public static BasicHttpBinding GetHttpBinding(this IConfiguration config, string key)
    {
        return GetHttpBinding(config[key]);
    }
    public static BasicHttpBinding GetHttpBinding(string uri)
    {
        return uri.StartsWithIgnoreCase("https") ? Https : Http;
    }
}
Related