C#-实现安全Web套接字
我想问你是否知道如何用.Net实现安全的Web套接字。 我已经实现了ws://和一切正常,但我不知道如何切换到wss://。 提前谢谢。你可以试试 Fleck是用C语言实现的WebSocket服务器# 从他们的例子来看:C#-实现安全Web套接字,c#,sockets,websocket,ssl-certificate,protocols,C#,Sockets,Websocket,Ssl Certificate,Protocols,我想问你是否知道如何用.Net实现安全的Web套接字。 我已经实现了ws://和一切正常,但我不知道如何切换到wss://。 提前谢谢。你可以试试 Fleck是用C语言实现的WebSocket服务器# 从他们的例子来看: var server = new WebSocketServer("wss://0.0.0.0:8431"); server.Certificate = new X509Certificate2("MyCert.pfx"); server.Start(socket =>
var server = new WebSocketServer("wss://0.0.0.0:8431");
server.Certificate = new X509Certificate2("MyCert.pfx");
server.Start(socket =>
{
//...use as normal
});
你可以试试
Fleck是用C语言实现的WebSocket服务器#
从他们的例子来看:
var server = new WebSocketServer("wss://0.0.0.0:8431");
server.Certificate = new X509Certificate2("MyCert.pfx");
server.Start(socket =>
{
//...use as normal
});
这个问题已经很老了,但下面是我如何让我的C#服务器接受来自客户端的SSL连接(在Chrome/Firefox上运行的js代码) 假设您已经拥有一个由可信CA(在我的例子中是letsencrypt.org,它允许您免费请求证书)签名的有效证书(在我的例子中是在我的Apache Web服务器上为SSL服务的同一证书),这是工作代码的摘录:
public static X509Certificate2 serverCertificate = null;
public Server(string ip_addr, int port)
{
serverCertificate = GetCertificateFromStore("CN=mydomain.com");
string resultsTrue = serverCertificate.ToString(true); // Debugging purposes
bool hasPrivateKey = serverCertificate.HasPrivateKey; // Debugging purposes (MUST return true)
Console.WriteLine("Certificate validation results: " + resultsTrue);
Console.WriteLine("Has private key? " + hasPrivateKey);
server = new TcpListener(IPAddress.Parse(ip_addr), port);
server.Start();
Console.WriteLine("Server has started on ip: " + ip_addr + ":"+port + " - Waiting for a connection...", Environment.NewLine);
}
public class ClientHandler
{
TcpClient client { get; set; }
//NetworkStream stream { get; set; } // Old plain non-secure tcp stream
SslStream stream { get; set; } // New secure tcp stream
....
public ClientHandler(TcpClient client, string room_id)
{
....
stream = new SslStream(client.GetStream(), false);
try
{
stream.AuthenticateAsServer(Server.serverCertificate, clientCertificateRequired: false, checkCertificateRevocation: false);
// Set timeouts for the read and write to 5 seconds.
stream.ReadTimeout = 5000;
stream.WriteTimeout = 5000;
}
catch (Exception ex)
{
Console.WriteLine("Error during SSL authentication with the client:" + ex);
return;
}
}
}
棘手的部分是,类需要从本地密钥库而不是从文件中检索证书。
此外,SSL还需要证书文件和私钥才能工作
我正在Linux和Mono.Net上开发,但在其他平台上不会有太大变化。我需要的工具是:openssl和certmgr(mono证书管理器)
要创建包含证书和私钥的.pfx文件,请执行以下操作:
openssl pkcs12 -export -in yourcertfile.cer -inkey yourprivatekeyfile.pem -out finalfile.pfx
要将获取的文件添加到我的本地存储中,请执行以下操作:
certmgr -add -c -m Trust finalfile.pfx
最后,您可以编辑客户端连接代码,以指向托管服务器的同一域(该域应该是证书中报告的同一域)
这:
变成:
var mySocket = new WebSocket("wss://yourdomain.com:5050");
请记住,一旦实现了SSL,就必须修改整个网络代码,因为这会给TCP流增加开销,在解析字节和位以查找和解码报头时必须考虑到这一点。
这就是我自己陷入困境的地方,但除此之外,SSL连接工作得很好:)这个问题很老了,但下面是我如何让我的C#服务器接受来自客户端的SSL连接(在Chrome/Firefox上运行的js代码) 假设您已经拥有一个由可信CA(在我的例子中是letsencrypt.org,它允许您免费请求证书)签名的有效证书(在我的例子中是在我的Apache Web服务器上为SSL服务的同一证书),这是工作代码的摘录:
public static X509Certificate2 serverCertificate = null;
public Server(string ip_addr, int port)
{
serverCertificate = GetCertificateFromStore("CN=mydomain.com");
string resultsTrue = serverCertificate.ToString(true); // Debugging purposes
bool hasPrivateKey = serverCertificate.HasPrivateKey; // Debugging purposes (MUST return true)
Console.WriteLine("Certificate validation results: " + resultsTrue);
Console.WriteLine("Has private key? " + hasPrivateKey);
server = new TcpListener(IPAddress.Parse(ip_addr), port);
server.Start();
Console.WriteLine("Server has started on ip: " + ip_addr + ":"+port + " - Waiting for a connection...", Environment.NewLine);
}
public class ClientHandler
{
TcpClient client { get; set; }
//NetworkStream stream { get; set; } // Old plain non-secure tcp stream
SslStream stream { get; set; } // New secure tcp stream
....
public ClientHandler(TcpClient client, string room_id)
{
....
stream = new SslStream(client.GetStream(), false);
try
{
stream.AuthenticateAsServer(Server.serverCertificate, clientCertificateRequired: false, checkCertificateRevocation: false);
// Set timeouts for the read and write to 5 seconds.
stream.ReadTimeout = 5000;
stream.WriteTimeout = 5000;
}
catch (Exception ex)
{
Console.WriteLine("Error during SSL authentication with the client:" + ex);
return;
}
}
}
棘手的部分是,类需要从本地密钥库而不是从文件中检索证书。
此外,SSL还需要证书文件和私钥才能工作
我正在Linux和Mono.Net上开发,但在其他平台上不会有太大变化。我需要的工具是:openssl和certmgr(mono证书管理器)
要创建包含证书和私钥的.pfx文件,请执行以下操作:
openssl pkcs12 -export -in yourcertfile.cer -inkey yourprivatekeyfile.pem -out finalfile.pfx
要将获取的文件添加到我的本地存储中,请执行以下操作:
certmgr -add -c -m Trust finalfile.pfx
最后,您可以编辑客户端连接代码,以指向托管服务器的同一域(该域应该是证书中报告的同一域)
这:
变成:
var mySocket = new WebSocket("wss://yourdomain.com:5050");
请记住,一旦实现了SSL,就必须修改整个网络代码,因为这会给TCP流增加开销,在解析字节和位以查找和解码报头时必须考虑到这一点。
这就是我自己陷入困境的地方,但除此之外,SSL连接工作得很好:)阅读您正在使用的库,了解如何添加SSL安全性阅读您正在使用的库,了解如何添加SSL安全性