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

查看:513
本文介绍了如何使用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类文档和Internet(甚至在这里)都没有对我有太大帮助。

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.

所以,这是我的问题:


  1. 是否可以通过我的代码启用它?就像从商店读取证书并以某种方式在HttpListener上设置它一样?

  2. 如果没有,httpcfg.exe是解决方案?如果是,如何使用它?

  3. 如果1 ==否和2 ==是,如何在Azure中使用httpcfg?

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

Thanks! I really appreciate the help...

最诚挚的问候...

Gutemberg

Gutemberg

推荐答案

确定,

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

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

httpcfg.exe在Windows之前的Windows版本上使用Vista。由于我使用的是.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}

更改 IP PORT 到要使用证书保护的所需IP地址和PORT编号,以及 YOUR_CERT_HASH 和证书的缩略图(证书应为以前上传到azure门户中)和 GUID ,以及您从任何GUID生成工具(如Visual Studio中的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天全站免登陆