如何使用 System.Net.HttpListener 在 WebSocket 中启用 SSL [英] How to enable SSL in WebSocket with System.Net.HttpListener

查看:32
本文介绍了如何使用 System.Net.HttpListener 在 WebSocket 中启用 SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows Azure(一个辅助角色)上使用 .Net 4.5 和 HttpListener 类来制作我自己的 WebSocket 服务器.对于 HTTP 非安全连接,它工作得很好.问题是我需要使用 SSL(wss://) 来保护 WebSocket 连接.

I'm using .Net 4.5 and HttpListener class on Windows Azure(a worker role) to make my own WebSocket server. For HTTP non-secure connections it works very well. The problem is that I need to make WebSocket connection secured with SSL(wss://).

我四处寻找,但 HttpListener 类文档和互联网(即使在这里)都对我没有太大帮助.

I've seeking around and neither HttpListener class documentation and internet(even here) hasn't helped me so much.

我发现 httpcfg.exe 应该可以帮助我配置证书,但运气不佳.

I found that httpcfg.exe should help me configure the certificate but no lucky.

所以,这就是我的问题:

So, that are my questions:

  1. 有什么办法可以从我的代码中启用它吗?喜欢从商店读取证书并将其设置在 HttpListener 上?
  2. 如果没有,httpcfg.exe 是解决方案吗?如果有,如何使用?
  3. 如果 1 == No 和 2 == yes,我如何在 Azure 中使用 httpcfg?

谢谢!我非常感谢您的帮助...

Thanks! I really appreciate the help...

最好的问候...

古腾堡

推荐答案

好的,刚刚想通了.

由于 HttpListenerHTTP.sys 的包装器,我们必须在开始使用 HttpListener 之前对其进行配置.

Since HttpListener is a wrapper to HTTP.sys, we must configure it before start using HttpListener.

httpcfg.exe 用于 Windows Vista 之前的 Windows 版本.由于我使用的是 .net 4.5 HttpListener WebSockets 并且它仅适用于 Windows8/windows Server 2012(如果您尝试在 Windows8 之前的 Windows 版本上使用 websockets,您将获得 PlattaformNotSupportedException),我们应该使用 netsh 实用程序而不是 httpcfg.

The httpcfg.exe is used on windows versions prior to Windows Vista. Since I'm using .net 4.5 HttpListener WebSockets and it only works on Windows8/windows Server 2012(if you try use websockets on pre-windows8 versions of windows, you will get a PlattaformNotSupportedException), we should use netsh utility instead of httpcfg.

因此,要在 Windows 8 中配置 HTTP.sys,请打开 CMD 并向其添加以下命令:

So, to configure HTTP.sys in Windows 8 open CMD and add the following command to it:

netsh http add sslcert ipport=IP:PORT certhash=YOU_CERT_HASH appid={GUID}

IPPORT 更改为您希望使用证书和YOUR_CERT_HASH 保护的所需 IP 地址和端口号,使用您的证书的指纹(该证书应事先上传到 azure 门户)和 GUID 以及您从任何 GUID 生成工具(例如 Visual Studio 的 GUID 生成工具(工具 > 创建 GUID))生成的 GUID.

Change IP and PORT to the desired IP address and PORT number that you want secure withing your certificate and YOUR_CERT_HASH with the thumbprint of your certificate(the cert should be previous uploaded into azure portal) and GUID with a GUID you generate from any GUID generation tool like the one from Visual Studio(Tools > Create Guid).

之后,你只需要设置HttpListener.Prefix.Add("https://youIP:youPort/yourPath")就可以了.

After it, you just need to set the HttpListener.Prefix.Add("https://youIP:youPort/yourPath") and you should be good.

像我一样,有些人会问如何使其在 Windows Azure 上运行...您可以在角色开始之前使用 启动任务 来运行此命令并根据需要设置 HTTP.sys.

Like me, some people will ask how to make it work on Windows Azure... You can use the Startup tasks to run this command before your role is started and setup HTTP.sys as needed.

有关如何使用启动任务的更多信息,请查看 MSDN http://msdn.microsoft.com/en-us/library/windowsazure/hh180155.aspx 以及有关 httpcfg 的更多详细信息netsh,请查看 MSDN http://msdn.microsoft 中的其他文档.com/en-us/library/ms733791.aspx

For more information on how to use startup tasks, check this documentation on MSDN http://msdn.microsoft.com/en-us/library/windowsazure/hh180155.aspx and for more details on httpcfg or netsh, check this other doc in MSDN http://msdn.microsoft.com/en-us/library/ms733791.aspx

这篇关于如何使用 System.Net.HttpListener 在 WebSocket 中启用 SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆