【问题标题】:Https web service in .NET problem.NET 中的 Https Web 服务问题
【发布时间】:2011-08-04 16:31:56
【问题描述】:

我是 .NET 和 SSL 的新手,我面临着在 SOAP 服务和客户端之间建立安全通信的问题。 它在 HTTP 上运行良好,但现在我们必须应用 SSL。 我们从 CA 获得证书。 接下来的步骤如下:

1) 使用httpcfg set ssl -i 0.0.0.0:777 -h <thumbprintkey>启用服务使用的端口

2)httpcfg set urlacl -u @987654321@ -a D:(A;;GA;;;AN)

3)httpcfg set iplisten-i 0.0.0.0:777

WebService 代码如下:

WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Message.ClientCredentialType = MessageCredentialType.None;

ServiceHost host = new ServiceHost(typeof(DeviceObservationConsumer_hostPCDData), new Uri("https://<domainname>:777/TlsService/ServiceSecure"));
host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByThumbprint, (string)"bd 35 ec c0 e6 b3 9a ac 74 09 09 c5 84 b8 fd 58 51 44 87 7d");

host.AddServiceEndpoint(typeof(IDeviceObservationConsumer_Binding_Soap12), binding, "");

ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
    smb = new ServiceMetadataBehavior();
smb.HttpsGetEnabled = true;

host.Description.Behaviors.Add(smb);

// Add MEX endpoint
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");

host.Open();

客户端代码如下:

WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Message.ClientCredentialType = MessageCredentialType.None;

EndpointAddress addr = new EndpointAddress("https://<domainname>:777/TlsService/ServiceSecure");

ChannelFactory<DeviceObservationConsumer_PortType> myChannelFactory = new ChannelFactory<DeviceObservationConsumer_PortType>(binding, addr);
//myChannelFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByThumbprint, (string)"bd 35 ec c0 e6 b3 9a ac 74 09 09 c5 84 b8 fd 58 51 44 87 7d");

DeviceObservationConsumer_PortType client = myChannelFactory.CreateChannel();

CommunicatePCDDataRequest req = new CommunicatePCDDataRequest("ciao mamma guarda come mi diverto!");
CommunicatePCDDataResponse resp = client.CommunicatePCDData(req);

myChannelFactory.Close();

服务和客户端都在同一台电脑上。

我无法通过浏览器访问https://&lt;domanname&gt;:777/TlsService/ServiceSecure(获取未找到页面错误),并且客户端肯定也无法访问它。 哪位大神能帮帮我!!!!??

提前致谢,

刘德米拉

【问题讨论】:

    标签: wcf ssl https


    【解决方案1】:

    我的理解是 WSHttpBinding 使用 HTTP 作为传输协议。要使用 HTTPS,您必须指定 HTTPS 传输协议。

    我建议您尝试使用自定义绑定(而不是预配置的 WSHttpBinding)和 HttpsTransportBindingElement。

    您的配置将类似于,

    <customBinding>
        <binding name="myBinding">
            <reliabileSession />
            <security><!--Your security section falls in here -->
            </security>
            <httpsTransport/>
            <textMessageEncoding />
        </binding>
    </customBinding>
    

    您可以参考customBinding了解更多信息。

    【讨论】:

    • 您好,感谢您的回复。我们没有使用配置文件。
    • 我们更改了绑定,但仍然出现相同的错误..."向 https://:777/TlsService/ServiceSecure 发出 HTTP 请求时发生错误。这可能是由于事实上,在 HTTPS 情况下,服务器证书没有正确配置 HTTP.SYS。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。"
    • ServiceHost host = new ServiceHost(typeof(DeviceObservationConsumer_hostPCDData), new Uri("https://:777/TlsService/ServiceSecure")); CustomBinding tlsbinding = new CustomBinding(); tlsbinding.Name = "TlsBinding"; HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement(); tlsbinding.Elements.Add(httpsTransport);
    【解决方案2】:

    这是我们这次使用的代码: 服务:

    ServiceHost host = new ServiceHost(typeof(DeviceObservationConsumer_hostPCDData), new Uri("https://<PCname>:777/TlsService/ServiceSecure"));
    CustomBinding tlsbinding = new CustomBinding();
    tlsbinding.Name = "TlsBinding";
    
    HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
    tlsbinding.Elements.Add(httpsTransport);
    
    ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
    // If not, add one
    if (sdb == null)
        sdb = new ServiceDebugBehavior();
    sdb.IncludeExceptionDetailInFaults = true;
    
    ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
    // If not, add one
    if (smb == null)
        smb = new ServiceMetadataBehavior();
    smb.HttpsGetEnabled = true;
    
    host.Description.Behaviors.Add(smb);
    host.Description.Behaviors.Add(sdb);
    // Add MEX endpoint
    host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");
    // Add application endpoint
    host.AddServiceEndpoint(typeof(IDeviceObservationConsumer_Binding_Soap12), tlsbinding, "");
    
    host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByThumbprint, (string)"bd 35 ec c0 e6 b3 9a ac 74 09 09 c5 84 b8 fd 58 51 44 87 7d");
    // hosting the WS
    host.Open();
    

    客户:

    CustomBinding binding = new CustomBinding();
    binding.Name = "TlsBinding";
    
    HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
    binding.Elements.Add(httpsTransport);
    
    EndpointAddress addr = new EndpointAddress("https://<PCname>:777/TlsService/ServiceSecure");
    
    ChannelFactory<DeviceObservationConsumer_PortType> myChannelFactory = new ChannelFactory<DeviceObservationConsumer_PortType>(binding, addr);
    
    DeviceObservationConsumer_PortType client = myChannelFactory.CreateChannel();
    
    CommunicatePCDDataRequest req = new CommunicatePCDDataRequest("ciao mamma guarda come mi diverto!");
    CommunicatePCDDataResponse resp = client.CommunicatePCDData(req);
    
    myChannelFactory.Close();
    

    已完成其余步骤以使 SSL 看起来适合您?

    再次感谢您的回复!

    刘德米拉

    【讨论】:

    • 不,它仍然无法工作:向 https://:777/TlsService/ServiceSecure 发出 HTTP 请求时出错。这可能是由于在 HTTPS 情况下未使用 HTTP.SYS 正确配置服务器证书。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。
    • 嗨 Mila,从高层次上看,客户端似乎没有向服务器提供证书信息,也没有协商证书。您想尝试从公开的元数据创建客户端代理吗? msdn.microsoft.com/en-us/library/ms733133.aspx
    • 我想知道的另一件事是您是否检查过服务是否以您想要的方式公开元数据?检查 https://:777/TlsService/ServiceSecure/mex 是否为您提供了有效的 WSDL。如果是这样,从它创建的客户端代理可能对您有用。
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多