Ajax call to wcf windows service over ssl (https)

孤街浪徒 提交于 2020-01-15 15:59:24

问题


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!


回答1:


I'm having the same problem here. It seems that you can't do ajax calls to https page while your current page is http. Just like you can't do calls to other domains. It's violation of SOP http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_SOP



来源:https://stackoverflow.com/questions/2392622/ajax-call-to-wcf-windows-service-over-ssl-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!