通过 ssl (https) Ajax 调用 wcf windows 服务

发布于 2024-08-24 01:52:37 字数 2631 浏览 5 评论 0原文

我有一个通过 http 公开端点的 Windows 服务。同样,这是一个 Windows 服务(不是 iis 中托管的 Web 服务)。然后,我使用 javascript/ajax 从此端点调用方法。一切都很完美,这是我在 Windows 服务中使用的代码来创建端点:

        //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();

现在,我的目标是让这个相同的模型通过 SSL(https 而不是 http)工作。因此,我遵循了几个 msdn 页面的指导,如下所示:

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

我使用 makecert.exe 创建了一个名为“bpCertTest”的测试证书。然后,我使用 netsh.exe 使用我创建的测试证书配置我的端口(1213),一切都没有问题。然后,我修改了 Windows 服务中的端点代码,以便能够通过 https 工作,如下所示:

        //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();

服务成功创建端点,在 SetCertificate() 调用中识别我的证书,并且服务成功启动并运行。

现在,问题是我的 javascript/ajax 调用无法通过 https 与服务进行通信。我只是收到一些通用通信错误(12031)。因此,作为测试,我将在 javascript 中调用的端口更改为其他随机端口,并且得到了相同的错误 - 这告诉我,我显然甚至没有通过 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: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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2024-08-31 01:52:37

我这里也有同样的问题。当当前页面是 http 时,您似乎无法对 https 页面进行 ajax 调用。就像您无法调用其他域一样。这违反了 SOP http://code。 google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_SOP

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文