Reuben
Reuben

Reputation: 4266

How do I programmatically specify HTTPS for BasicHttpBinding?

I'm trying to consume a web service in .NET 4 (Visual Studio 2010) that requires HTTPS and is authenticated with a client certificate. Currently, I'm writing a console program, just to prove the concept, but I'm having trouble with having the program recognise that it should be using https. At least that is what the error suggests:

An error occurred while making the HTTP request to https://. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be cause by a mismatch of the security binding between the client and the server.

Here's the sample code I am using :

class Program
{
    static void Main(string[] args)
    {
        try
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

            Uri baseAddress = new Uri("https://<host>:<port>/<endpoint>");

            var certificate = new X509Certificate2("<localpath to certificate>.p12", "<password>");
            EndpointAddress endpointAddress = new EndpointAddress(baseAddress);

            ChannelFactory<LinePortType> factory = new ChannelFactory<LinePortType>(binding, endpointAddress);
            factory.Credentials.ClientCertificate.Certificate = certificate;
            LinePortType proxy = factory.CreateChannel();


            var header = new ctSoapHeaderMsg();
            var response = new object();
            var request = new PerformServiceRequest(header, "<string>");
            var responseCode = proxy.PerformService(request);

            Console.WriteLine("Response Code :" + responseCode.ToString());
            Console.WriteLine("Response :" + response.ToString());

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

        Console.ReadLine(); // Pause
    }
}

I've replaced a few of the sensitive strings with placeholders.

It's entirely possibly that the issue may be with me not being able to configure the certificate properly. Eventually, I hope to have this loaded from a local certificate store, so I don't have to specify the password in the code or configuration.

The LinePortType is a Service Reference based on a local WSDL. Because the WSDL was for a production environment, I'm changing the endpoint to reference a UAT environment, instead.


On the recommendation from srsyogesh, I've updated to use WSHttpBinding, but I'm still getting the same error.

The code inside the try is now looking like:

var binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

var baseAddress = new Uri("https://<host>:<port>/<endpoint>");
var endpointAddress = new EndpointAddress(baseAddress);

var client = new LinePortTypeClient(binding, endpointAddress);

client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByIssuerName, "<issuername>");

var header = new ctSoapHeaderMsg();
var response = new object();
var responseCode = client.PerformTelstraSQ(ref header, "<string>", out response);

Console.WriteLine("Response Code :" + responseCode);
Console.WriteLine("Response :" + response);

Upvotes: 2

Views: 21861

Answers (2)

Stoyan Bonchev
Stoyan Bonchev

Reputation: 537

You should NOT use the BasicHttpBinding, but BasicHttpsBinding. It will solve the problem I guess.

Upvotes: 5

srsyogesh
srsyogesh

Reputation: 609

You can use HTTPS with WsHttpBinding and not with BasicHttpBinding. Make sure that your server is started with the configuration that you would like to use and then try to connect from client.

Upvotes: 0

Related Questions