首页 > 解决方案 > WCF WebSocket 服务需要安全 TLS/SSL WSS

问题描述

我们有一个用于 WebSockets 的现有 WCF Web 服务。我们将我们的解决方案分发给在 IIS 中托管应用程序的客户。我的任务是让服务使用端口 443 (WSS/HTTPS)。我已尝试更新 customBinding 的 Web.Config 以使用 httpsTransport,但我无法使用 wss:// 成功连接到服务。我已将我们应用程序的简化示例上传到使用 ws://的https://github.com/kevmoens/WcfWebSocketService 。

这是更新的 Web.Config 以尝试使用 httpsTransport

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.2" />
    <httpRuntime targetFramework="4.6.2"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfWebSocketService.ConnectService"
               behaviorConfiguration="WcfWebSocketService.ConnectService">
        <endpoint address=""
                  binding="customBinding"
                  bindingConfiguration="webSocket"
                  contract="WcfWebSocketService.IConnectService"/>
      </service>
    </services>
    <bindings>
      <customBinding>
        <binding name="webSocket">
          <byteStreamMessageEncoding />
          <httpsTransport>
            <webSocketSettings transportUsage="Always"
                               createNotificationOnConnection="true"
                               />
          </httpsTransport>
        </binding>
      </customBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfWebSocketService.ConnectService">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

如果我将其更改为使用 WS://,这是一个示例客户端。

<!doctype html>
<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
    <meta content="utf-8" http-equiv="encoding" />
    <meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=0.5, user-scalable=0" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>Web Socket Demo</title>
    <style type="text/css">
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font: 13px Helvetica, Arial; }
        form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
        form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
        form button { width: 9%; background: rgb(130, 200, 255); border: none; padding: 10px; }
        #messages { list-style-type: none; margin: 0; padding: 0; }
        #messages li { padding: 5px 10px; }
        #messages li:nth-child(odd) { background: #eee; }
    </style>
</head>
<body>
    <ul id="messages"></ul>
    <form action="">
    <input id="txtBox" autocomplete="off" /><button>Send</button>
    </form>

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.js" ></script>
    <script type="text/javascript">

        var CONNECTION;

        window.onload = function () {
            // open the connection to the Web Socket server
            CONNECTION = new WebSocket('wss://localhost:51462/ConnectService.svc');


            // When the connection is open
            CONNECTION.onopen = function () {
                $('#messages').append($('<li>').text('Connection opened'));
            };

            // when the connection is closed by the server
            CONNECTION.onclose = function () {
                $('#messages').append($('<li>').text('Connection closed'));
            };

            // Log errors
            CONNECTION.onerror = function (e) {
                console.log('An error occured');
            };

            // Log messages from the server
            CONNECTION.onmessage = function (e) {
                $('#messages').append($('<li>').text(e.data));
            };
        };
        
        // when we press the Send button, send the text to the server
        $('form').submit(function(){
            CONNECTION.send($('#txtBox').val());
            $('#txtBox').val('');
            return false;
        });
        
    </script>
</body>
</html>

标签: wcfsslwebsocket

解决方案


我发现客户端在使用 IIS 并需要 SSL 时无法使用 localhost。我假设 httpTransport 需要更改为 httpsTransport 是正确的。此外,我不需要在 Web.Config 中添加任何关于证书的内容,因为它从 IIS 继承了证书。

客户

 CONNECTION = new WebSocket('wss://localhost/ConnectService.svc');

 CONNECTION = new WebSocket('wss://<MACHINENAME>/ConnectService.svc');

网页配置

          <httpTransport>
            <webSocketSettings transportUsage="Always"
                               createNotificationOnConnection="true"
                               />
          </httpTransport>

          <httpsTransport>
            <webSocketSettings transportUsage="Always"
                               createNotificationOnConnection="true"
                               />
          </httpsTransport>

推荐阅读