Ajax call to wcf windows service over ssl (https)

Posted by bpatrick100 on Stack Overflow See other posts from Stack Overflow or by bpatrick100
Published on 2010-03-06T13:36:25Z Indexed on 2010/04/05 1:43 UTC
Read the original article Hit count: 848

Filed under:
|
|
|

I have a windows service which exposes an endpoint over http. Again this is a windows service (not a web service hosted in iis). I then call methods from this endpoint, using javascript/ajax. Everything works perfectly, and this the code I'm using in my windows service to create the endpoint:

        //Create host object
        WebServiceHost webServiceHost = new WebServiceHost(svcHost.obj, new Uri("http://192.168.0.100:1213"));

        //Add Https Endpoint
        WebHttpBinding binding = new WebHttpBinding();
        webServiceHost.AddServiceEndpoint(svcHost.serviceContract, binding, string.Empty);


        //Add MEX Behaivor and EndPoint
        ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
        metadataBehavior.HttpGetEnabled = true;
        webServiceHost.Description.Behaviors.Add(metadataBehavior);
        webServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

        webServiceHost.Open();

Now, my goal is to get this same model working over SSL (https not http). So, I have followed the guidance of several msdn pages, like the following:

http://msdn.microsoft.com/en-us/library/ms733791(VS.100).aspx

I have used makecert.exe to create a test cert called "bpCertTest". I have then used netsh.exe to configure my port (1213) with the test cert I created, all with no problem. Then, I've modified the endpoint code in my windows service to be able to work over https as follows:

        //Create host object
        WebServiceHost webServiceHost = new WebServiceHost(svcHost.obj, new Uri("https://192.168.0.100:1213"));

        //Add Https Endpoint
        WebHttpBinding binding = new WebHttpBinding();
        binding.Security.Mode = WebHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        webServiceHost.AddServiceEndpoint(svcHost.serviceContract, binding, string.Empty);
        webServiceHost.Credentials.ServiceCertificate.SetCertificate("CN=bpCertTest", StoreLocation.LocalMachine, StoreName.My);


        //Add MEX Behaivor and EndPoint
        ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
        metadataBehavior.HttpsGetEnabled = true;
        webServiceHost.Description.Behaviors.Add(metadataBehavior);
        webServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");

        webServiceHost.Open();

The service creates the endpoint successfully, recognizes my cert in the SetCertificate() call, and the service starts up and running with success.

Now, the problem is my javascript/ajax call cannot communicate with the service over https. I simply get some generic commication error (12031). So, as a test, I changed the port I was calling in the javascript to some other random port, and I get the same error - which tells me that I'm obviously not even reaching my service over https.

I'm at a complete loss at this point, I feel like everything is in place, and I just can't see what the problem is. If anyone has experience in this scenario, please provide your insight and/or solution!

Thanks!

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about wcf