Ajax call to wcf windows service over ssl (https)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bpatrick100
    New Member
    • Mar 2010
    • 2

    #1

    Ajax call to wcf windows service over ssl (https)

    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:1 213"));

    //Add Https Endpoint
    WebHttpBinding binding = new WebHttpBinding( );
    webServiceHost. AddServiceEndpo int(svcHost.ser viceContract, binding, string.Empty);


    //Add MEX Behaivor and EndPoint
    ServiceMetadata Behavior metadataBehavio r = new ServiceMetadata Behavior();
    metadataBehavio r.HttpGetEnable d = true;
    webServiceHost. Description.Beh aviors.Add(meta dataBehavior);
    webServiceHost. AddServiceEndpo int(ServiceMeta dataBehavior.Me xContractName, MetadataExchang eBindings.Creat eMexHttpBinding (), "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, which I've included here:

    http://msdn.microsoft. com/en-us/library/ms733791.aspx
    http://msdn.microsoft. com/en-us/library/ms788967.aspx
    http://msdn.microsoft. com/en-us/library/ms731925.aspx
    http://msdn.microsoft. com/en-us/library/ms729700.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:1 213"));

    //Add Https Endpoint
    WebHttpBinding binding = new WebHttpBinding( );
    binding.Securit y.Mode = WebHttpSecurity Mode.Transport;
    binding.Securit y.Transport.Cli entCredentialTy pe = HttpClientCrede ntialType.Certi ficate;
    webServiceHost. AddServiceEndpo int(svcHost.ser viceContract, binding, string.Empty);
    webServiceHost. Credentials.Ser viceCertificate .SetCertificate ("CN=bpCertTest ", StoreLocation.L ocalMachine, StoreName.My);


    //Add MEX Behaivor and EndPoint
    ServiceMetadata Behavior metadataBehavio r = new ServiceMetadata Behavior();
    metadataBehavio r.HttpsGetEnabl ed = true;
    webServiceHost. Description.Beh aviors.Add(meta dataBehavior);
    webServiceHost. AddServiceEndpo int(ServiceMeta dataBehavior.Me xContractName, MetadataExchang eBindings.Creat eMexHttpsBindin g(), "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!
Working...