C# WebSocketServer wss ://connection fail

标签 c# ssl browser websocket

我写了一个 WebSocket 服务器,目前只能使用 ws://连接。
使用 http://www.websocket.org/echo.html 测试(在 Chrome 上)和 http://websocket4net.codeplex.com/ .

现在我还想支持 wss:// 连接 (TLS)。
我已经让它与 WebSocket4Net 一起工作,但是在使用回声测试时,
在 C# 代码中执行 SslStream.AuthenticateAsServer(); 后,javascript 立即抛出异常。

它进行 TLS 协商,但 Chrome 拒绝连接。
SslStream 读写(ServerSide 由 AuthenticateAsServer 完成):

Read(5, 0, 5) == 5
Read(517, 5, 512) == 512
Write(887, 0, 887)
Read(887, 0, 5) == 5
Read(887, 5, 262) == 262
Read(5, 0, 5) == 5
Read(6, 5, 1) == 1
Read(5, 0, 5) == 5
Read(53, 5, 48) == 48
Write(59, 0, 59)

我使用以下步骤添加证书:

makecert -sv CA.pvk -r -n "CN=Dev" -a sha256 -len 2048 -sky signature -cy authority CA.cer
makecert -ic CA.cer -iv CA.pvk -n "CN=localhost, CN=127.0.0.1" -a sha256 -len 2048 -sky exchange -sv CA_localhost.pvk -pe CA_localhost.cer
cert2spc CA_localhost.cer CA_localhost.spc
pvkimprt -pfx CA_localhost.spc CA_localhost.pvk //Select export
//Import CA.cer into your Computer store's Trusted Root Certification Authorities (certmgr.msc)
//Import the private key that the server is going to use into the server machine's Personal store.
//This is achieved by importing the .pfx file that you generated earlier.

new SslStream(new NetworkStream(clientSocket), true)
创建的 SslStream 对于 AuthenticateAsServer,我使用的是“CA_localhost.cer”证书。

在确保证书显示在 certmgr.msc 中后,我打开了 http://www.websocket.org/echo.html在 Chrome 中并使用 wss://localhost:12345 作为地址(我的服务器正在监听的地址)并选中复选框。
单击 Connect 后,我只得到:

 ERROR: undefined
 DISCONNECTED

javascript控制台显示:
与“wss://127.0.0.1:12345/?encoding=text”的 WebSocket 连接失败:连接在收到握手响应之前关闭

AuthenticateAsServer 之后的服务器端,属性 IsAuthenticatedIsEncrypted 被设置为 true,但在那之后我立即收到零长度的数据,关闭连接。

我假设我在证书创建和/或安装过程中犯了一个错误,但我不知道我可能做错了什么。
有什么想法吗?

最佳答案

经过几个小时的测试和放弃,我又试了一次:
事实证明(或者至少对我来说它看起来像)只有最后一个通用名称被用来验证 ws-uri。

使用 wss://127.0.0.1:12345 有效。

关于C# WebSocketServer wss ://connection fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21989800/

相关文章:

c# - 在字符串列表中选择一个字符串 linq

c# - 序列不包含任何元素

java - 如何使用 GET REST API 调用传递 ssl 证书

php - 在用户登录期间/之后强制 SSL 连接

windows - 为什么 Fiddler 不显示 curl 流量?

c# - 如何在 .NET 4.5 中与 Windows 运行时互操作

c# - 为什么 ReadAllLines 在 WPF 中有效但在 ConsoleApp 中无效

javascript - 浏览器重新评估和重新应用 CSS 选择器和样式的条件是什么?

javascript - 如何为静态托管网站设置 Cloud Firestore

javascript - 查询Internet Explorer单个选项卡的内存使用情况