【问题标题】:WCF WebSocket Service needs to be secure TLS/SSL WSSWCF WebSocket 服务需要安全 TLS/SSL WSS
【发布时间】:2020-12-05 23:17:35
【问题描述】:

我们有一个用于 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>

【问题讨论】:

  • 使用https协议时需要指定SSL证书,但是在你的配置节点中没有配置。您可以参考下面的链接来配置证书。 docs.microsoft.com/en-us/dotnet/framework/wcf/samples/…
  • @TheobaldDu 既然托管在 IIS 中,并且网站上的绑定设置为证书,WCF 不会自动获取证书吗?同样重要的是自签名证书而不是来自受信任机构的证书吗?

标签: wcf ssl websocket


【解决方案1】:

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

客户

之前

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

之后

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

WEB.CONFIG

之前

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

之后

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

【讨论】:

    猜你喜欢
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2018-08-03
    • 2017-04-27
    • 2012-11-04
    • 2011-08-25
    • 1970-01-01
    相关资源
    最近更新 更多