如何使用SSL加密,用C#实现SignalR客户端和服务器端的通信

r1zhe5dt  于 2024-01-08  发布在  C#
关注(0)|答案(1)|浏览(360)

我有一个使用C#的SignalR客户端和服务器的基本连接代码由Net Framework实现如下:

客户端

  1. using Microsoft.AspNet.SignalR.Client;
  2. namespace WinFormsClient
  3. {
  4. public partial class FrmClient : Form
  5. {
  6. //Connection to a SignalR server
  7. HubConnection _signalRConnection;
  8. //Proxy object for a hub hosted on the SignalR server
  9. IHubProxy _hubProxy;
  10. private async Task connectAsync()
  11. {
  12. //Set Certificate callback
  13. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
  14. //Create a connection for the SignalR server
  15. _signalRConnection = new HubConnection("https://10.224.10.10:12316/signalr/SimpleHub/");
  16. _signalRConnection.StateChanged += HubConnection_StateChanged;
  17. _signalRConnection.Closed += HubConnection_Closed;
  18. //Get a proxy object that will be used to interact with the specific hub on the server
  19. //Ther may be many hubs hosted on the server, so provide the type name for the hub
  20. _hubProxy = _signalRConnection.CreateHubProxy("SimpleHub");
  21. //Reigster to the "AddMessage" callback method of the hub
  22. //This method is invoked by the hub
  23. _hubProxy.On<string, string>("AddMessage", (name, message) => writeToLog($"{name}:{message}"));
  24. try
  25. {
  26. //Connect to the server
  27. await _signalRConnection.Start();
  28. //Send user name for this client, so we won't need to send it with every message
  29. await _hubProxy.Invoke("SetUserName", txtUserName.Text);
  30. }
  31. catch (Exception ex)
  32. {
  33. writeToLog($"Error:{ex.Message}");
  34. }
  35. }
  36. private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErros)
  37. {
  38. return true;
  39. }
  40. }
  41. }

字符串

服务器

  1. using Microsoft.Owin.Hosting;
  2. using Microsoft.AspNet.SignalR;
  3. using System.ComponentModel;
  4. namespace WinFormsServer
  5. {
  6. public partial class FrmServer : Form
  7. {
  8. private IDisposable _signalR;
  9. private void btnStartServer_Click(object sender, EventArgs e)
  10. {
  11. try
  12. {
  13. //Start SignalR server with the give URL address
  14. //Final server address will be "URL/signalr"
  15. //Startup.Configuration is called automatically
  16. _signalR = WebApp.Start<Startup>("https://localhost:12316");
  17. writeToLog($"Server started at:{txtUrl.Text}");
  18. }
  19. catch (Exception ex)
  20. {
  21. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  22. }
  23. }
  24. }
  25. }
  26. using Owin;
  27. using Microsoft.Owin.Cors;
  28. namespace WinFormsServer
  29. {
  30. class Startup
  31. {
  32. public void Configuration(IAppBuilder app)
  33. {
  34. //CORS need to be enabled for calling SignalR service
  35. app.UseCors(CorsOptions.AllowAll);
  36. //Find and reigster SignalR hubs
  37. app.MapSignalR();
  38. }
  39. }
  40. }


现在,我将客户端和服务器部署在同一台机器上,并通过以下语句将自签名SSL证书绑定到指定的IP和端口。Netsh http add sslcert ipport=10.224.10.10:12316 certificate=xxxx appid={xxxx}
然后我尝试启动服务器,它正常运行。当我尝试启动客户端连接到服务器时,我被提示以下错误:

  1. StatusCode:400, ReasonPhrase:'Bad Request', Version: 1.1, Content: System.Net.Http.SteamContent, Headers:
  2. {
  3. Connection: close
  4. Data: Mon, 20 Nov 2023 04:41:15 GMT
  5. Server: Microsoft-HTTPAPI/2.0
  6. Content-Length:334
  7. Content-Type:text/html; charset=us-ascii
  8. }


我已将自签名证书导入MMS控制台根目录的个人和受信任根证书颁发机构,以及本地计算机和当前用户。我不知道为什么客户端无法连接到服务器。有人可以帮助我吗?
另外,如果将服务器端URL改为https://10.224.10.10:12316/,会提示服务器启动失败,内部异常HttpListenerException:Access is denied。我在本地的服务器规则中添加了服务名称和绑定端口12316,还需要配置什么吗?

11-21-2023最新更新

我已经找到了一个解决方案。通过以管理员身份运行服务器端,我可以使用公共IP启动SignalR服务器。
我有一个新问题。我已经配置了SignalR规则,但我无法从其他客户端计算机访问SignalR服务器。客户端使用以下URL请求连接。https://public IP:12316/signalr/SimpleHub/
错误响应为发送请求时发生错误。SocketException:连接尝试失败,因为连接的一段时间后没有正确响应,或者连接的主机没有响应,建立连接失败10.224.10.10:12316

i1icjdpr

i1icjdpr1#

我已经找到了一个解决方案。通过以管理员身份运行服务器端,我可以使用公共IP启动SignalR服务器。

相关问题